home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / gcc-2.003 / gcc-2 / gcc-2.7.0 / cccp.c next >
Encoding:
C/C++ Source or Header  |  1995-09-08  |  278.0 KB  |  10,458 lines

  1. /* C Compatible Compiler Preprocessor (CCCP)
  2.    Copyright (C) 1986, 87, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3.    Written by Paul Rubin, June 1986
  4.    Adapted to ANSI C, Richard Stallman, Jan 1987
  5.  
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, 59 Temple Place - Suite 330,
  19. Boston, MA 02111-1307, USA.
  20.  
  21.  In other words, you are welcome to use, share and improve this program.
  22.  You are forbidden to forbid anyone else to use, share and improve
  23.  what you give them.   Help stamp out software-hoarding!  */
  24.  
  25. typedef unsigned char U_CHAR;
  26.  
  27. #ifdef EMACS
  28. #define NO_SHORTNAMES
  29. #include "../src/config.h"
  30. #ifdef open
  31. #undef open
  32. #undef read
  33. #undef write
  34. #endif /* open */
  35. #endif /* EMACS */
  36.  
  37. /* The macro EMACS is defined when cpp is distributed as part of Emacs,
  38.    for the sake of machines with limited C compilers.  */
  39. #ifndef EMACS
  40. #include "config.h"
  41. #endif /* not EMACS */
  42.  
  43. #ifndef STANDARD_INCLUDE_DIR
  44. #define STANDARD_INCLUDE_DIR "/usr/include"
  45. #endif
  46.  
  47. #ifndef LOCAL_INCLUDE_DIR
  48. #define LOCAL_INCLUDE_DIR "/usr/local/include"
  49. #endif
  50.  
  51. #if 0 /* We can't get ptrdiff_t, so I arranged not to need PTR_INT_TYPE.  */
  52. #ifdef __STDC__
  53. #define PTR_INT_TYPE ptrdiff_t
  54. #else
  55. #define PTR_INT_TYPE long
  56. #endif
  57. #endif /* 0 */
  58.  
  59. #include "pcp.h"
  60.  
  61. /* By default, colon separates directories in a path.  */
  62. #ifndef PATH_SEPARATOR
  63. #define PATH_SEPARATOR ':'
  64. #endif
  65.  
  66. #include <sys/types.h>
  67. #include <sys/stat.h>
  68. #include <ctype.h>
  69. #include <stdio.h>
  70. #include <signal.h>
  71.  
  72. /* The following symbols should be autoconfigured:
  73.     HAVE_FCNTL_H
  74.     HAVE_STDLIB_H
  75.     HAVE_SYS_TIME_H
  76.     HAVE_UNISTD_H
  77.     STDC_HEADERS
  78.     TIME_WITH_SYS_TIME
  79.    In the mean time, we'll get by with approximations based
  80.    on existing GCC configuration symbols.  */
  81.  
  82. #ifdef POSIX
  83. # ifndef HAVE_STDLIB_H
  84. # define HAVE_STDLIB_H 1
  85. # endif
  86. # ifndef HAVE_UNISTD_H
  87. # define HAVE_UNISTD_H 1
  88. # endif
  89. # ifndef STDC_HEADERS
  90. # define STDC_HEADERS 1
  91. # endif
  92. #endif /* defined (POSIX) */
  93.  
  94. #if defined (POSIX) || (defined (USG) && !defined (VMS))
  95. # ifndef HAVE_FCNTL_H
  96. # define HAVE_FCNTL_H 1
  97. # endif
  98. #endif
  99.  
  100. #ifndef RLIMIT_STACK
  101. # include <time.h>
  102. #else
  103. # if TIME_WITH_SYS_TIME
  104. #  include <sys/time.h>
  105. #  include <time.h>
  106. # else
  107. #  if HAVE_SYS_TIME_H
  108. #   include <sys/time.h>
  109. #  else
  110. #   include <time.h>
  111. #  endif
  112. # endif
  113. # include <sys/resource.h>
  114. #endif
  115.  
  116. #if HAVE_FCNTL_H
  117. # include <fcntl.h>
  118. #endif
  119.  
  120. /* This defines "errno" properly for VMS, and gives us EACCES. */
  121. #include <errno.h>
  122.  
  123. #if HAVE_STDLIB_H
  124. # include <stdlib.h>
  125. #else
  126. char *getenv ();
  127. #endif
  128.  
  129. #if STDC_HEADERS
  130. # include <string.h>
  131. # ifndef bcmp
  132. # define bcmp(a, b, n) memcmp (a, b, n)
  133. # endif
  134. # ifndef bcopy
  135. # define bcopy(s, d, n) memcpy (d, s, n)
  136. # endif
  137. # ifndef bzero
  138. # define bzero(d, n) memset (d, 0, n)
  139. # endif
  140. #else /* !STDC_HEADERS */
  141. char *index ();
  142. char *rindex ();
  143.  
  144. # if !defined (BSTRING) && (defined (USG) || defined (VMS))
  145.  
  146. #  ifndef bcmp
  147. #  define bcmp my_bcmp
  148. static int
  149. my_bcmp (a, b, n)
  150.      register char *a;
  151.      register char *b;
  152.      register unsigned n;
  153. {
  154.    while (n-- > 0)
  155.      if (*a++ != *b++)
  156.        return 1;
  157.  
  158.    return 0;
  159. }
  160. #  endif /* !defined (bcmp) */
  161.  
  162. #  ifndef bcopy
  163. #  define bcopy my_bcopy
  164. static void
  165. my_bcopy (s, d, n)
  166.      register char *s;
  167.      register char *d;
  168.      register unsigned n;
  169. {
  170.   while (n-- > 0)
  171.     *d++ = *s++;
  172. }
  173. #  endif /* !defined (bcopy) */
  174.  
  175. #  ifndef bzero
  176. #  define bzero my_bzero
  177. static void
  178. my_bzero (b, length)
  179.      register char *b;
  180.      register unsigned length;
  181. {
  182.   while (length-- > 0)
  183.     *b++ = 0;
  184. }
  185. #  endif /* !defined (bzero) */
  186.  
  187. # endif /* !defined (BSTRING) && (defined (USG) || defined (VMS)) */
  188. #endif /* ! STDC_HEADERS */
  189.  
  190. #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 6)
  191. # define __attribute__(x)
  192. #endif
  193.  
  194. #ifndef PROTO
  195. # if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  196. #  define PROTO(ARGS) ARGS
  197. # else
  198. #  define PROTO(ARGS) ()
  199. # endif
  200. #endif
  201.  
  202. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  203. # include <stdarg.h>
  204. # define VA_START(va_list, var) va_start (va_list, var)
  205. # define PRINTF_ALIST(msg) char *msg, ...
  206. # define PRINTF_DCL(msg)
  207. # define PRINTF_PROTO(ARGS, m, n) PROTO (ARGS) __attribute__ ((format (printf, m, n)))
  208. #else
  209. # include <varargs.h>
  210. # define VA_START(va_list, var) va_start (va_list)
  211. # define PRINTF_ALIST(msg) msg, va_alist
  212. # define PRINTF_DCL(msg) char *msg; va_dcl
  213. # define PRINTF_PROTO(ARGS, m, n) () __attribute__ ((format (printf, m, n)))
  214. # define vfprintf(file, msg, args) \
  215.     { \
  216.       char *a0 = va_arg(args, char *); \
  217.       char *a1 = va_arg(args, char *); \
  218.       char *a2 = va_arg(args, char *); \
  219.       char *a3 = va_arg(args, char *); \
  220.       fprintf (file, msg, a0, a1, a2, a3); \
  221.     }
  222. #endif
  223.  
  224. #define PRINTF_PROTO_1(ARGS) PRINTF_PROTO(ARGS, 1, 2)
  225. #define PRINTF_PROTO_2(ARGS) PRINTF_PROTO(ARGS, 2, 3)
  226. #define PRINTF_PROTO_3(ARGS) PRINTF_PROTO(ARGS, 3, 4)
  227.  
  228. #if HAVE_UNISTD_H
  229. # include <unistd.h>
  230. #endif
  231.  
  232. /* VMS-specific definitions */
  233. #ifdef VMS
  234. #include <descrip.h>
  235. #define O_RDONLY    0    /* Open arg for Read/Only  */
  236. #define O_WRONLY    1    /* Open arg for Write/Only */
  237. #define read(fd,buf,size)    VMS_read (fd,buf,size)
  238. #define write(fd,buf,size)    VMS_write (fd,buf,size)
  239. #define open(fname,mode,prot)    VMS_open (fname,mode,prot)
  240. #define fopen(fname,mode)    VMS_fopen (fname,mode)
  241. #define freopen(fname,mode,ofile) VMS_freopen (fname,mode,ofile)
  242. #define strncat(dst,src,cnt) VMS_strncat (dst,src,cnt)
  243. #define fstat(fd,stbuf)        VMS_fstat (fd,stbuf)
  244. static int VMS_fstat (), VMS_stat ();
  245. static char * VMS_strncat ();
  246. static int VMS_read ();
  247. static int VMS_write ();
  248. static int VMS_open ();
  249. static FILE * VMS_fopen ();
  250. static FILE * VMS_freopen ();
  251. static void hack_vms_include_specification ();
  252. typedef struct { unsigned :16, :16, :16; } vms_ino_t;
  253. #define ino_t vms_ino_t
  254. #define INCLUDE_LEN_FUDGE 10    /* leave room for VMS syntax conversion */
  255. #ifdef __GNUC__
  256. #define BSTRING            /* VMS/GCC supplies the bstring routines */
  257. #endif /* __GNUC__ */
  258. #endif /* VMS */
  259.  
  260. #ifndef O_RDONLY
  261. #define O_RDONLY 0
  262. #endif
  263.  
  264. #undef MIN
  265. #undef MAX
  266. #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
  267. #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
  268.  
  269. /* Find the largest host integer type and set its size and type.  */
  270.  
  271. #ifndef HOST_BITS_PER_WIDE_INT
  272.  
  273. #if HOST_BITS_PER_LONG > HOST_BITS_PER_INT
  274. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_LONG
  275. #define HOST_WIDE_INT long
  276. #else
  277. #define HOST_BITS_PER_WIDE_INT HOST_BITS_PER_INT
  278. #define HOST_WIDE_INT int
  279. #endif
  280.  
  281. #endif
  282.  
  283. #ifndef S_ISREG
  284. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  285. #endif
  286.  
  287. #ifndef S_ISDIR
  288. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  289. #endif
  290.  
  291. /* Define a generic NULL if one hasn't already been defined.  */
  292.  
  293. #ifndef NULL
  294. #define NULL 0
  295. #endif
  296.  
  297. #ifndef GENERIC_PTR
  298. #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
  299. #define GENERIC_PTR void *
  300. #else
  301. #define GENERIC_PTR char *
  302. #endif
  303. #endif
  304.  
  305. #ifndef NULL_PTR
  306. #define NULL_PTR ((GENERIC_PTR)0)
  307. #endif
  308.  
  309. #ifndef INCLUDE_LEN_FUDGE
  310. #define INCLUDE_LEN_FUDGE 0
  311. #endif
  312.  
  313. /* External declarations.  */
  314.  
  315. extern char *version_string;
  316. #ifndef VMS
  317. #ifndef HAVE_STRERROR
  318. extern int sys_nerr;
  319. #if defined(bsd4_4)
  320. extern const char *const sys_errlist[];
  321. #else
  322. extern char *sys_errlist[];
  323. #endif
  324. #else    /* HAVE_STRERROR */
  325. char *strerror ();
  326. #endif
  327. #else    /* VMS */
  328. char *strerror (int,...);
  329. #endif
  330. int parse_escape PROTO((char **));
  331. HOST_WIDE_INT parse_c_expression PROTO((char *));
  332.  
  333. #ifndef errno
  334. extern int errno;
  335. #endif
  336.  
  337. /* Name under which this program was invoked.  */
  338.  
  339. static char *progname;
  340.  
  341. /* Nonzero means use extra default include directories for C++.  */
  342.  
  343. static int cplusplus;
  344.  
  345. /* Nonzero means handle cplusplus style comments */
  346.  
  347. static int cplusplus_comments;
  348.  
  349. /* Nonzero means handle #import, for objective C.  */
  350.  
  351. static int objc;
  352.  
  353. /* Nonzero means this is an assembly file, and allow
  354.    unknown directives, which could be comments.  */
  355.  
  356. static int lang_asm;
  357.  
  358. /* Current maximum length of directory names in the search path
  359.    for include files.  (Altered as we get more of them.)  */
  360.  
  361. static int max_include_len;
  362.  
  363. /* Nonzero means turn NOTREACHED into #pragma NOTREACHED etc */
  364.  
  365. static int for_lint = 0;
  366.  
  367. /* Nonzero means copy comments into the output file.  */
  368.  
  369. static int put_out_comments = 0;
  370.  
  371. /* Nonzero means don't process the ANSI trigraph sequences.  */
  372.  
  373. static int no_trigraphs = 0;
  374.  
  375. /* Nonzero means replace MSDOS CRLF sequences. */
  376.  
  377. static int no_msdoscr = 0;
  378.  
  379. /* Nonzero means print the names of included files rather than
  380.    the preprocessed output.  1 means just the #include "...",
  381.    2 means #include <...> as well.  */
  382.  
  383. static int print_deps = 0;
  384.  
  385. /* Nonzero if missing .h files in -M output are assumed to be generated
  386.    files and not errors.  */
  387.  
  388. static int print_deps_missing_files = 0;
  389.  
  390. /* Nonzero means print names of header files (-H).  */
  391.  
  392. static int print_include_names = 0;
  393.  
  394. /* Nonzero means don't output line number information.  */
  395.  
  396. static int no_line_directives;
  397.  
  398. /* Nonzero means output the text in failing conditionals,
  399.    inside #failed ... #endfailed.  */
  400.  
  401. static int output_conditionals;
  402.  
  403. /* dump_only means inhibit output of the preprocessed text
  404.              and instead output the definitions of all user-defined
  405.              macros in a form suitable for use as input to cccp.
  406.    dump_names means pass #define and the macro name through to output.
  407.    dump_definitions means pass the whole definition (plus #define) through
  408. */
  409.  
  410. static enum {dump_none, dump_only, dump_names, dump_definitions}
  411.      dump_macros = dump_none;
  412.  
  413. /* Nonzero means pass all #define and #undef directives which we actually
  414.    process through to the output stream.  This feature is used primarily
  415.    to allow cc1 to record the #defines and #undefs for the sake of
  416.    debuggers which understand about preprocessor macros, but it may
  417.    also be useful with -E to figure out how symbols are defined, and
  418.    where they are defined.  */
  419. static int debug_output = 0;
  420.  
  421. /* Nonzero indicates special processing used by the pcp program.  The
  422.    special effects of this mode are: 
  423.      
  424.      Inhibit all macro expansion, except those inside #if directives.
  425.  
  426.      Process #define directives normally, and output their contents 
  427.      to the output file.
  428.  
  429.      Output preconditions to pcp_outfile indicating all the relevant
  430.      preconditions for use of this file in a later cpp run.
  431. */
  432. static FILE *pcp_outfile;
  433.  
  434. /* Nonzero means we are inside an IF during a -pcp run.  In this mode
  435.    macro expansion is done, and preconditions are output for all macro
  436.    uses requiring them. */
  437. static int pcp_inside_if;
  438.  
  439. /* Nonzero means never to include precompiled files.
  440.    This is 1 since there's no way now to make precompiled files,
  441.    so it's not worth testing for them.  */
  442. static int no_precomp = 1;
  443.  
  444. /* Nonzero means give all the error messages the ANSI standard requires.  */
  445.  
  446. int pedantic;
  447.  
  448. /* Nonzero means try to make failure to fit ANSI C an error.  */
  449.  
  450. static int pedantic_errors;
  451.  
  452. /* Nonzero means don't print warning messages.  -w.  */
  453.  
  454. static int inhibit_warnings = 0;
  455.  
  456. /* Nonzero means warn if slash-star appears in a comment.  */
  457.  
  458. static int warn_comments;
  459.  
  460. /* Nonzero means warn if a macro argument is (or would be)
  461.    stringified with -traditional.  */
  462.  
  463. static int warn_stringify;
  464.  
  465. /* Nonzero means warn if there are any trigraphs.  */
  466.  
  467. static int warn_trigraphs;
  468.  
  469. /* Nonzero means warn if #import is used.  */
  470.  
  471. static int warn_import = 1;
  472.  
  473. /* Nonzero means turn warnings into errors.  */
  474.  
  475. static int warnings_are_errors;
  476.  
  477. /* Nonzero means try to imitate old fashioned non-ANSI preprocessor.  */
  478.  
  479. int traditional;
  480.  
  481. /* Nonzero causes output not to be done,
  482.    but directives such as #define that have side effects
  483.    are still obeyed.  */
  484.  
  485. static int no_output;
  486.  
  487. /* Nonzero means this file was included with a -imacros or -include
  488.    command line and should not be recorded as an include file.  */
  489.  
  490. static int no_record_file;
  491.  
  492. /* Nonzero means that we have finished processing the command line options.
  493.    This flag is used to decide whether or not to issue certain errors
  494.    and/or warnings.  */
  495.  
  496. static int done_initializing = 0;
  497.  
  498. /* Line where a newline was first seen in a string constant.  */
  499.  
  500. static int multiline_string_line = 0;
  501.  
  502. /* I/O buffer structure.
  503.    The `fname' field is nonzero for source files and #include files
  504.    and for the dummy text used for -D and -U.
  505.    It is zero for rescanning results of macro expansion
  506.    and for expanding macro arguments.  */
  507. #define INPUT_STACK_MAX 400
  508. static struct file_buf {
  509.   char *fname;
  510.   /* Filename specified with #line directive.  */
  511.   char *nominal_fname;
  512.   /* Record where in the search path this file was found.
  513.      For #include_next.  */
  514.   struct file_name_list *dir;
  515.   int lineno;
  516.   int length;
  517.   U_CHAR *buf;
  518.   U_CHAR *bufp;
  519.   /* Macro that this level is the expansion of.
  520.      Included so that we can reenable the macro
  521.      at the end of this level.  */
  522.   struct hashnode *macro;
  523.   /* Value of if_stack at start of this file.
  524.      Used to prohibit unmatched #endif (etc) in an include file.  */
  525.   struct if_stack *if_stack;
  526.   /* Object to be freed at end of input at this level.  */
  527.   U_CHAR *free_ptr;
  528.   /* True if this is a header file included using <FILENAME>.  */
  529.   char system_header_p;
  530. } instack[INPUT_STACK_MAX];
  531.  
  532. static int last_error_tick;       /* Incremented each time we print it.  */
  533. static int input_file_stack_tick;  /* Incremented when the status changes.  */
  534.  
  535. /* Current nesting level of input sources.
  536.    `instack[indepth]' is the level currently being read.  */
  537. static int indepth = -1;
  538. #define CHECK_DEPTH(code) \
  539.   if (indepth >= (INPUT_STACK_MAX - 1))                    \
  540.     {                                    \
  541.       error_with_line (line_for_error (instack[indepth].lineno),    \
  542.                "macro or `#include' recursion too deep");    \
  543.       code;                                \
  544.     }
  545.  
  546. /* Current depth in #include directives that use <...>.  */
  547. static int system_include_depth = 0;
  548.  
  549. typedef struct file_buf FILE_BUF;
  550.  
  551. /* The output buffer.  Its LENGTH field is the amount of room allocated
  552.    for the buffer, not the number of chars actually present.  To get
  553.    that, subtract outbuf.buf from outbuf.bufp. */
  554.  
  555. #define OUTBUF_SIZE 10    /* initial size of output buffer */
  556. static FILE_BUF outbuf;
  557.  
  558. /* Grow output buffer OBUF points at
  559.    so it can hold at least NEEDED more chars.  */
  560.  
  561. #define check_expand(OBUF, NEEDED)  \
  562.   (((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED))   \
  563.    ? grow_outbuf ((OBUF), (NEEDED)) : 0)
  564.  
  565. struct file_name_list
  566.   {
  567.     struct file_name_list *next;
  568.     char *fname;
  569.     /* If the following is nonzero, it is a macro name.
  570.        Don't include the file again if that macro is defined.  */
  571.     U_CHAR *control_macro;
  572.     /* If the following is nonzero, it is a C-language system include
  573.        directory.  */
  574.     int c_system_include_path;
  575.     /* Mapping of file names for this directory.  */
  576.     struct file_name_map *name_map;
  577.     /* Non-zero if name_map is valid.  */
  578.     int got_name_map;
  579.   };
  580.  
  581. /* #include "file" looks in source file dir, then stack. */
  582. /* #include <file> just looks in the stack. */
  583. /* -I directories are added to the end, then the defaults are added. */
  584. /* The */
  585. static struct default_include {
  586.   char *fname;            /* The name of the directory.  */
  587.   int cplusplus;        /* Only look here if we're compiling C++.  */
  588.   int cxx_aware;        /* Includes in this directory don't need to
  589.                    be wrapped in extern "C" when compiling
  590.                    C++.  */
  591. } include_defaults_array[]
  592. #ifdef INCLUDE_DEFAULTS
  593.   = INCLUDE_DEFAULTS;
  594. #else
  595.   = {
  596.     /* Pick up GNU C++ specific include files.  */
  597.     { GPLUSPLUS_INCLUDE_DIR, 1, 1 },
  598. #ifdef CROSS_COMPILE
  599.     /* This is the dir for fixincludes.  Put it just before
  600.        the files that we fix.  */
  601.     { GCC_INCLUDE_DIR, 0, 0 },
  602.     /* For cross-compilation, this dir name is generated
  603.        automatically in Makefile.in.  */
  604.     { CROSS_INCLUDE_DIR, 0, 0 },
  605.     /* This is another place that the target system's headers might be.  */
  606.     { TOOL_INCLUDE_DIR, 0, 0 },
  607. #else /* not CROSS_COMPILE */
  608.     /* This should be /usr/local/include and should come before
  609.        the fixincludes-fixed header files.  */
  610.     { LOCAL_INCLUDE_DIR, 0, 1 },
  611.     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
  612.        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
  613.     { TOOL_INCLUDE_DIR, 0, 0 },
  614.     /* This is the dir for fixincludes.  Put it just before
  615.        the files that we fix.  */
  616.     { GCC_INCLUDE_DIR, 0, 0 },
  617.     /* Some systems have an extra dir of include files.  */
  618. #ifdef SYSTEM_INCLUDE_DIR
  619.     { SYSTEM_INCLUDE_DIR, 0, 0 },
  620. #endif
  621.     { STANDARD_INCLUDE_DIR, 0, 0 },
  622. #endif /* not CROSS_COMPILE */
  623.     { 0, 0, 0 }
  624.     };
  625. #endif /* no INCLUDE_DEFAULTS */
  626.  
  627. /* The code looks at the defaults through this pointer, rather than through
  628.    the constant structure above.  This pointer gets changed if an environment
  629.    variable specifies other defaults.  */
  630. static struct default_include *include_defaults = include_defaults_array;
  631.  
  632. static struct file_name_list *include = 0;    /* First dir to search */
  633.     /* First dir to search for <file> */
  634. /* This is the first element to use for #include <...>.
  635.    If it is 0, use the entire chain for such includes.  */
  636. static struct file_name_list *first_bracket_include = 0;
  637. /* This is the first element in the chain that corresponds to
  638.    a directory of system header files.  */
  639. static struct file_name_list *first_system_include = 0;
  640. static struct file_name_list *last_include = 0;    /* Last in chain */
  641.  
  642. /* Chain of include directories to put at the end of the other chain.  */
  643. static struct file_name_list *after_include = 0;
  644. static struct file_name_list *last_after_include = 0;    /* Last in chain */
  645.  
  646. /* Chain to put at the start of the system include files.  */
  647. static struct file_name_list *before_system = 0;
  648. static struct file_name_list *last_before_system = 0;    /* Last in chain */
  649.  
  650. /* List of included files that contained #pragma once.  */
  651. static struct file_name_list *dont_repeat_files = 0;
  652.  
  653. /* List of other included files.
  654.    If ->control_macro if nonzero, the file had a #ifndef
  655.    around the entire contents, and ->control_macro gives the macro name.  */
  656. static struct file_name_list *all_include_files = 0;
  657.  
  658. /* Directory prefix that should replace `/usr' in the standard
  659.    include file directories.  */
  660. static char *include_prefix;
  661.  
  662. /* Global list of strings read in from precompiled files.  This list
  663.    is kept in the order the strings are read in, with new strings being
  664.    added at the end through stringlist_tailp.  We use this list to output
  665.    the strings at the end of the run. 
  666. */
  667. static STRINGDEF *stringlist;
  668. static STRINGDEF **stringlist_tailp = &stringlist;
  669.  
  670.  
  671. /* Structure returned by create_definition */
  672. typedef struct macrodef MACRODEF;
  673. struct macrodef
  674. {
  675.   struct definition *defn;
  676.   U_CHAR *symnam;
  677.   int symlen;
  678. };
  679.  
  680. enum sharp_token_type {
  681.   NO_SHARP_TOKEN = 0,        /* token not present */
  682.  
  683.   SHARP_TOKEN = '#',        /* token spelled with # only */
  684.   WHITE_SHARP_TOKEN,        /* token spelled with # and white space */
  685.  
  686.   PERCENT_COLON_TOKEN = '%',    /* token spelled with %: only */
  687.   WHITE_PERCENT_COLON_TOKEN    /* token spelled with %: and white space */
  688. };
  689.  
  690. /* Structure allocated for every #define.  For a simple replacement
  691.    such as
  692.        #define foo bar ,
  693.    nargs = -1, the `pattern' list is null, and the expansion is just
  694.    the replacement text.  Nargs = 0 means a functionlike macro with no args,
  695.    e.g.,
  696.        #define getchar() getc (stdin) .
  697.    When there are args, the expansion is the replacement text with the
  698.    args squashed out, and the reflist is a list describing how to
  699.    build the output from the input: e.g., "3 chars, then the 1st arg,
  700.    then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
  701.    The chars here come from the expansion.  Whatever is left of the
  702.    expansion after the last arg-occurrence is copied after that arg.
  703.    Note that the reflist can be arbitrarily long---
  704.    its length depends on the number of times the arguments appear in
  705.    the replacement text, not how many args there are.  Example:
  706.    #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
  707.    pattern list
  708.      { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
  709.    where (x, y) means (nchars, argno). */
  710.  
  711. typedef struct definition DEFINITION;
  712. struct definition {
  713.   int nargs;
  714.   int length;            /* length of expansion string */
  715.   int predefined;        /* True if the macro was builtin or */
  716.                 /* came from the command line */
  717.   U_CHAR *expansion;
  718.   int line;            /* Line number of definition */
  719.   char *file;            /* File of definition */
  720.   char rest_args;        /* Nonzero if last arg. absorbs the rest */
  721.   struct reflist {
  722.     struct reflist *next;
  723.  
  724.     enum sharp_token_type stringify;    /* set if a # operator before arg */
  725.     enum sharp_token_type raw_before;    /* set if a ## operator before arg */
  726.     enum sharp_token_type raw_after;    /* set if a ## operator after arg */
  727.  
  728.     char rest_args;        /* Nonzero if this arg. absorbs the rest */
  729.     int nchars;            /* Number of literal chars to copy before
  730.                    this arg occurrence.  */
  731.     int argno;            /* Number of arg to substitute (origin-0) */
  732.   } *pattern;
  733.   union {
  734.     /* Names of macro args, concatenated in reverse order
  735.        with comma-space between them.
  736.        The only use of this is that we warn on redefinition
  737.        if this differs between the old and new definitions.  */
  738.     U_CHAR *argnames;
  739.   } args;
  740. };
  741.  
  742. /* different kinds of things that can appear in the value field
  743.    of a hash node.  Actually, this may be useless now. */
  744. union hashval {
  745.   char *cpval;
  746.   DEFINITION *defn;
  747.   KEYDEF *keydef;
  748. };
  749.  
  750. /*
  751.  * special extension string that can be added to the last macro argument to 
  752.  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
  753.  *         #define wow(a, b...)        process (b, a, b)
  754.  *        { wow (1, 2, 3); }    ->    { process (2, 3, 1, 2, 3); }
  755.  *        { wow (one, two); }    ->    { process (two, one, two); }
  756.  * if this "rest_arg" is used with the concat token '##' and if it is not
  757.  * supplied then the token attached to with ## will not be outputted.  Ex:
  758.  *         #define wow (a, b...)        process (b ## , a, ## b)
  759.  *        { wow (1, 2); }        ->    { process (2, 1, 2); }
  760.  *        { wow (one); }        ->    { process (one); {
  761.  */
  762. static char rest_extension[] = "...";
  763. #define REST_EXTENSION_LENGTH    (sizeof (rest_extension) - 1)
  764.  
  765. /* The structure of a node in the hash table.  The hash table
  766.    has entries for all tokens defined by #define directives (type T_MACRO),
  767.    plus some special tokens like __LINE__ (these each have their own
  768.    type, and the appropriate code is run when that type of node is seen.
  769.    It does not contain control words like "#define", which are recognized
  770.    by a separate piece of code. */
  771.  
  772. /* different flavors of hash nodes --- also used in keyword table */
  773. enum node_type {
  774.  T_DEFINE = 1,    /* the `#define' keyword */
  775.  T_INCLUDE,    /* the `#include' keyword */
  776.  T_INCLUDE_NEXT, /* the `#include_next' keyword */
  777.  T_IMPORT,      /* the `#import' keyword */
  778.  T_IFDEF,    /* the `#ifdef' keyword */
  779.  T_IFNDEF,    /* the `#ifndef' keyword */
  780.  T_IF,        /* the `#if' keyword */
  781.  T_ELSE,    /* `#else' */
  782.  T_PRAGMA,    /* `#pragma' */
  783.  T_ELIF,    /* `#elif' */
  784.  T_UNDEF,    /* `#undef' */
  785.  T_LINE,    /* `#line' */
  786.  T_ERROR,    /* `#error' */
  787.  T_WARNING,    /* `#warning' */
  788.  T_ENDIF,    /* `#endif' */
  789.  T_SCCS,    /* `#sccs', used on system V.  */
  790.  T_IDENT,    /* `#ident', used on system V.  */
  791.  T_ASSERT,    /* `#assert', taken from system V.  */
  792.  T_UNASSERT,    /* `#unassert', taken from system V.  */
  793.  T_SPECLINE,    /* special symbol `__LINE__' */
  794.  T_DATE,    /* `__DATE__' */
  795.  T_FILE,    /* `__FILE__' */
  796.  T_BASE_FILE,    /* `__BASE_FILE__' */
  797.  T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
  798.  T_VERSION,    /* `__VERSION__' */
  799.  T_SIZE_TYPE,   /* `__SIZE_TYPE__' */
  800.  T_PTRDIFF_TYPE,   /* `__PTRDIFF_TYPE__' */
  801.  T_WCHAR_TYPE,   /* `__WCHAR_TYPE__' */
  802.  T_USER_LABEL_PREFIX_TYPE, /* `__USER_LABEL_PREFIX__' */
  803.  T_REGISTER_PREFIX_TYPE,   /* `__REGISTER_PREFIX__' */
  804.  T_IMMEDIATE_PREFIX_TYPE,  /* `__IMMEDIATE_PREFIX__' */
  805.  T_TIME,    /* `__TIME__' */
  806.  T_CONST,    /* Constant value, used by `__STDC__' */
  807.  T_MACRO,    /* macro defined by `#define' */
  808.  T_DISABLED,    /* macro temporarily turned off for rescan */
  809.  T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
  810.  T_PCSTRING,    /* precompiled string (hashval is KEYDEF *) */
  811.  T_UNUSED    /* Used for something not defined.  */
  812.  };
  813.  
  814. struct hashnode {
  815.   struct hashnode *next;    /* double links for easy deletion */
  816.   struct hashnode *prev;
  817.   struct hashnode **bucket_hdr;    /* also, a back pointer to this node's hash
  818.                    chain is kept, in case the node is the head
  819.                    of the chain and gets deleted. */
  820.   enum node_type type;        /* type of special token */
  821.   int length;            /* length of token, for quick comparison */
  822.   U_CHAR *name;            /* the actual name */
  823.   union hashval value;        /* pointer to expansion, or whatever */
  824. };
  825.  
  826. typedef struct hashnode HASHNODE;
  827.  
  828. /* Some definitions for the hash table.  The hash function MUST be
  829.    computed as shown in hashf () below.  That is because the rescan
  830.    loop computes the hash value `on the fly' for most tokens,
  831.    in order to avoid the overhead of a lot of procedure calls to
  832.    the hashf () function.  Hashf () only exists for the sake of
  833.    politeness, for use when speed isn't so important. */
  834.  
  835. #define HASHSIZE 1403
  836. static HASHNODE *hashtab[HASHSIZE];
  837. #define HASHSTEP(old, c) ((old << 2) + c)
  838. #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
  839.  
  840. /* Symbols to predefine.  */
  841.  
  842. #ifdef CPP_PREDEFINES
  843. static char *predefs = CPP_PREDEFINES;
  844. #else
  845. static char *predefs = "";
  846. #endif
  847.  
  848. /* We let tm.h override the types used here, to handle trivial differences
  849.    such as the choice of unsigned int or long unsigned int for size_t.
  850.    When machines start needing nontrivial differences in the size type,
  851.    it would be best to do something here to figure out automatically
  852.    from other information what type to use.  */
  853.  
  854. /* The string value for __SIZE_TYPE__.  */
  855.  
  856. #ifndef SIZE_TYPE
  857. #define SIZE_TYPE "long unsigned int"
  858. #endif
  859.  
  860. /* The string value for __PTRDIFF_TYPE__.  */
  861.  
  862. #ifndef PTRDIFF_TYPE
  863. #define PTRDIFF_TYPE "long int"
  864. #endif
  865.  
  866. /* The string value for __WCHAR_TYPE__.  */
  867.  
  868. #ifndef WCHAR_TYPE
  869. #define WCHAR_TYPE "int"
  870. #endif
  871. char * wchar_type = WCHAR_TYPE;
  872. #undef WCHAR_TYPE
  873.  
  874. /* The string value for __USER_LABEL_PREFIX__ */
  875.  
  876. #ifndef USER_LABEL_PREFIX
  877. #define USER_LABEL_PREFIX ""
  878. #endif
  879.  
  880. /* The string value for __REGISTER_PREFIX__ */
  881.  
  882. #ifndef REGISTER_PREFIX
  883. #define REGISTER_PREFIX ""
  884. #endif
  885.  
  886. /* The string value for __IMMEDIATE_PREFIX__ */
  887.  
  888. #ifndef IMMEDIATE_PREFIX
  889. #define IMMEDIATE_PREFIX ""
  890. #endif
  891.  
  892. /* In the definition of a #assert name, this structure forms
  893.    a list of the individual values asserted.
  894.    Each value is itself a list of "tokens".
  895.    These are strings that are compared by name.  */
  896.  
  897. struct tokenlist_list {
  898.   struct tokenlist_list *next;
  899.   struct arglist *tokens;
  900. };
  901.  
  902. struct assertion_hashnode {
  903.   struct assertion_hashnode *next;    /* double links for easy deletion */
  904.   struct assertion_hashnode *prev;
  905.   /* also, a back pointer to this node's hash
  906.      chain is kept, in case the node is the head
  907.      of the chain and gets deleted. */
  908.   struct assertion_hashnode **bucket_hdr;
  909.   int length;            /* length of token, for quick comparison */
  910.   U_CHAR *name;            /* the actual name */
  911.   /* List of token-sequences.  */
  912.   struct tokenlist_list *value;
  913. };
  914.  
  915. typedef struct assertion_hashnode ASSERTION_HASHNODE;
  916.  
  917. /* Some definitions for the hash table.  The hash function MUST be
  918.    computed as shown in hashf below.  That is because the rescan
  919.    loop computes the hash value `on the fly' for most tokens,
  920.    in order to avoid the overhead of a lot of procedure calls to
  921.    the hashf function.  hashf only exists for the sake of
  922.    politeness, for use when speed isn't so important. */
  923.  
  924. #define ASSERTION_HASHSIZE 37
  925. static ASSERTION_HASHNODE *assertion_hashtab[ASSERTION_HASHSIZE];
  926.  
  927. /* Nonzero means inhibit macroexpansion of what seem to be
  928.    assertion tests, in rescan.  For #if.  */
  929. static int assertions_flag;
  930.  
  931. /* `struct directive' defines one #-directive, including how to handle it.  */
  932.  
  933. #define DO_PROTO PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *))
  934.  
  935. struct directive {
  936.   int length;            /* Length of name */
  937.   int (*func) DO_PROTO;    /* Function to handle directive */
  938.   char *name;            /* Name of directive */
  939.   enum node_type type;        /* Code which describes which directive. */
  940.   char angle_brackets;        /* Nonzero => <...> is special.  */
  941.   char traditional_comments;    /* Nonzero: keep comments if -traditional.  */
  942.   char pass_thru;        /* Copy preprocessed directive to output file.  */
  943. };
  944.  
  945. /* These functions are declared to return int instead of void since they
  946.    are going to be placed in the table and some old compilers have trouble with
  947.    pointers to functions returning void.  */
  948.  
  949. static int do_assert DO_PROTO;
  950. static int do_define DO_PROTO;
  951. static int do_elif DO_PROTO;
  952. static int do_else DO_PROTO;
  953. static int do_endif DO_PROTO;
  954. static int do_error DO_PROTO;
  955. static int do_ident DO_PROTO;
  956. static int do_if DO_PROTO;
  957. static int do_include DO_PROTO;
  958. static int do_line DO_PROTO;
  959. static int do_pragma DO_PROTO;
  960. #ifdef SCCS_DIRECTIVE
  961. static int do_sccs DO_PROTO;
  962. #endif
  963. static int do_unassert DO_PROTO;
  964. static int do_undef DO_PROTO;
  965. static int do_warning DO_PROTO;
  966. static int do_xifdef DO_PROTO;
  967.  
  968. /* Here is the actual list of #-directives, most-often-used first.  */
  969.  
  970. static struct directive directive_table[] = {
  971.   {  6, do_define, "define", T_DEFINE, 0, 1},
  972.   {  2, do_if, "if", T_IF},
  973.   {  5, do_xifdef, "ifdef", T_IFDEF},
  974.   {  6, do_xifdef, "ifndef", T_IFNDEF},
  975.   {  5, do_endif, "endif", T_ENDIF},
  976.   {  4, do_else, "else", T_ELSE},
  977.   {  4, do_elif, "elif", T_ELIF},
  978.   {  4, do_line, "line", T_LINE},
  979.   {  7, do_include, "include", T_INCLUDE, 1},
  980.   { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
  981.   {  6, do_include, "import", T_IMPORT, 1},
  982.   {  5, do_undef, "undef", T_UNDEF},
  983.   {  5, do_error, "error", T_ERROR},
  984.   {  7, do_warning, "warning", T_WARNING},
  985. #ifdef SCCS_DIRECTIVE
  986.   {  4, do_sccs, "sccs", T_SCCS},
  987. #endif
  988.   {  6, do_pragma, "pragma", T_PRAGMA, 0, 0, 1},
  989.   {  5, do_ident, "ident", T_IDENT},
  990.   {  6, do_assert, "assert", T_ASSERT},
  991.   {  8, do_unassert, "unassert", T_UNASSERT},
  992.   {  -1, 0, "", T_UNUSED},
  993. };
  994.  
  995. /* When a directive handler is called,
  996.    this points to the # (or the : of the %:) that started the directive.  */
  997. U_CHAR *directive_start;
  998.  
  999. /* table to tell if char can be part of a C identifier. */
  1000. U_CHAR is_idchar[256];
  1001. /* table to tell if char can be first char of a c identifier. */
  1002. U_CHAR is_idstart[256];
  1003. /* table to tell if c is horizontal space.  */
  1004. U_CHAR is_hor_space[256];
  1005. /* table to tell if c is horizontal or vertical space.  */
  1006. static U_CHAR is_space[256];
  1007. /* names of some characters */
  1008. static char *char_name[256];
  1009.  
  1010. #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
  1011. #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
  1012.   
  1013. static int errors = 0;            /* Error counter for exit code */
  1014.  
  1015. /* Name of output file, for error messages.  */
  1016. static char *out_fname;
  1017.  
  1018. /* Zero means dollar signs are punctuation.
  1019.    -$ stores 0; -traditional may store 1.  Default is 1 for VMS, 0 otherwise.
  1020.    This must be 0 for correct processing of this ANSI C program:
  1021.     #define foo(a) #a
  1022.     #define lose(b) foo (b)
  1023.     #define test$
  1024.     lose (test)    */
  1025. static int dollars_in_ident;
  1026. #ifndef DOLLARS_IN_IDENTIFIERS
  1027. #define DOLLARS_IN_IDENTIFIERS 1
  1028. #endif
  1029.  
  1030.  
  1031. /* Stack of conditionals currently in progress
  1032.    (including both successful and failing conditionals).  */
  1033.  
  1034. struct if_stack {
  1035.   struct if_stack *next;    /* for chaining to the next stack frame */
  1036.   char *fname;        /* copied from input when frame is made */
  1037.   int lineno;            /* similarly */
  1038.   int if_succeeded;        /* true if a leg of this if-group
  1039.                     has been passed through rescan */
  1040.   U_CHAR *control_macro;    /* For #ifndef at start of file,
  1041.                    this is the macro name tested.  */
  1042.   enum node_type type;        /* type of last directive seen in this group */
  1043. };
  1044. typedef struct if_stack IF_STACK_FRAME;
  1045. static IF_STACK_FRAME *if_stack = NULL;
  1046.  
  1047. /* Buffer of -M output.  */
  1048. static char *deps_buffer;
  1049.  
  1050. /* Number of bytes allocated in above.  */
  1051. static int deps_allocated_size;
  1052.  
  1053. /* Number of bytes used.  */
  1054. static int deps_size;
  1055.  
  1056. /* Number of bytes since the last newline.  */
  1057. static int deps_column;
  1058.  
  1059. /* Nonzero means -I- has been seen,
  1060.    so don't look for #include "foo" the source-file directory.  */
  1061. static int ignore_srcdir;
  1062.  
  1063. static int safe_read PROTO((int, char *, int));
  1064. static void safe_write PROTO((int, char *, int));
  1065.  
  1066. int main PROTO((int, char **));
  1067.  
  1068. static void path_include PROTO((char *));
  1069.  
  1070. static U_CHAR *index0 PROTO((U_CHAR *, int, size_t));
  1071.  
  1072. static void msdos_pcp PROTO((FILE_BUF *));
  1073.  
  1074. static void trigraph_pcp PROTO((FILE_BUF *));
  1075.  
  1076. static void newline_fix PROTO((U_CHAR *));
  1077. static void name_newline_fix PROTO((U_CHAR *));
  1078.  
  1079. static char *get_lintcmd PROTO((U_CHAR *, U_CHAR *, U_CHAR **, int *, int *));
  1080.  
  1081. static void rescan PROTO((FILE_BUF *, int));
  1082.  
  1083. static FILE_BUF expand_to_temp_buffer PROTO((U_CHAR *, U_CHAR *, int, int));
  1084.  
  1085. static int handle_directive PROTO((FILE_BUF *, FILE_BUF *));
  1086.  
  1087. static struct tm *timestamp PROTO((void));
  1088. static void special_symbol PROTO((HASHNODE *, FILE_BUF *));
  1089.  
  1090. static int redundant_include_p PROTO((char *));
  1091. static is_system_include PROTO((char *));
  1092.  
  1093. static char *read_filename_string PROTO((int, FILE *));
  1094. static struct file_name_map *read_name_map PROTO((char *));
  1095. static int open_include_file PROTO((char *, struct file_name_list *));
  1096.  
  1097. static void finclude PROTO((int, char *, FILE_BUF *, int, struct file_name_list *));
  1098. static void record_control_macro PROTO((char *, U_CHAR *));
  1099.  
  1100. static int import_hash PROTO((char *));
  1101. static int lookup_import PROTO((char *, struct file_name_list *));
  1102. static void add_import PROTO((int, char *));
  1103.  
  1104. static char *check_precompiled PROTO((int, char *, char **));
  1105. static int check_preconditions PROTO((char *));
  1106. static void pcfinclude PROTO((U_CHAR *, U_CHAR *, U_CHAR *, FILE_BUF *));
  1107. static void pcstring_used PROTO((HASHNODE *));
  1108. static void write_output PROTO((void));
  1109. static void pass_thru_directive PROTO((U_CHAR *, U_CHAR *, FILE_BUF *, struct directive *));
  1110.  
  1111. static MACRODEF create_definition PROTO((U_CHAR *, U_CHAR *, FILE_BUF *));
  1112.  
  1113. static int check_macro_name PROTO((U_CHAR *, char *));
  1114. static int compare_defs PROTO((DEFINITION *, DEFINITION *));
  1115. static int comp_def_part PROTO((int, U_CHAR *, int, U_CHAR *, int, int));
  1116.  
  1117. static DEFINITION *collect_expansion  PROTO((U_CHAR *, U_CHAR *, int, struct arglist *));
  1118.  
  1119. int check_assertion PROTO((U_CHAR *, int, int, struct arglist *));
  1120. static int compare_token_lists PROTO((struct arglist *, struct arglist *));
  1121.  
  1122. static struct arglist *read_token_list PROTO((U_CHAR **, U_CHAR *, int *));
  1123. static void free_token_list PROTO((struct arglist *));
  1124.  
  1125. static ASSERTION_HASHNODE *assertion_install PROTO((U_CHAR *, int, int));
  1126. static ASSERTION_HASHNODE *assertion_lookup PROTO((U_CHAR *, int, int));
  1127. static void delete_assertion PROTO((ASSERTION_HASHNODE *));
  1128.  
  1129. static void do_once PROTO((void));
  1130.  
  1131. static HOST_WIDE_INT eval_if_expression PROTO((U_CHAR *, int));
  1132. static void conditional_skip PROTO((FILE_BUF *, int, enum node_type, U_CHAR *, FILE_BUF *));
  1133. static void skip_if_group PROTO((FILE_BUF *, int, FILE_BUF *));
  1134. static void validate_else PROTO((U_CHAR *));
  1135.  
  1136. static U_CHAR *skip_to_end_of_comment PROTO((FILE_BUF *, int *, int));
  1137. static U_CHAR *skip_quoted_string PROTO((U_CHAR *, U_CHAR *, int, int *, int *, int *));
  1138. static char *quote_string PROTO((char *, char *));
  1139. static U_CHAR *skip_paren_group PROTO((FILE_BUF *));
  1140.  
  1141. /* Last arg to output_line_directive.  */
  1142. enum file_change_code {same_file, enter_file, leave_file};
  1143. static void output_line_directive PROTO((FILE_BUF *, FILE_BUF *, int, enum file_change_code));
  1144.  
  1145. static void macroexpand PROTO((HASHNODE *, FILE_BUF *));
  1146.  
  1147. struct argdata;
  1148. static char *macarg PROTO((struct argdata *, int));
  1149.  
  1150. static U_CHAR *macarg1 PROTO((U_CHAR *, U_CHAR *, int *, int *, int *, int));
  1151.  
  1152. static int discard_comments PROTO((U_CHAR *, int, int));
  1153.  
  1154. static int change_newlines PROTO((U_CHAR *, int));
  1155.  
  1156. char *my_strerror PROTO((int));
  1157. void error PRINTF_PROTO_1((char *, ...));
  1158. static void verror PROTO((char *, va_list));
  1159. static void error_from_errno PROTO((char *));
  1160. void warning PRINTF_PROTO_1((char *, ...));
  1161. static void vwarning PROTO((char *, va_list));
  1162. static void error_with_line PRINTF_PROTO_2((int, char *, ...));
  1163. static void verror_with_line PROTO((int, char *, va_list));
  1164. static void vwarning_with_line PROTO((int, char *, va_list));
  1165. void pedwarn PRINTF_PROTO_1((char *, ...));
  1166. void pedwarn_with_line PRINTF_PROTO_2((int, char *, ...));
  1167. static void pedwarn_with_file_and_line PRINTF_PROTO_3((char *, int, char *, ...));
  1168.  
  1169. static void print_containing_files PROTO((void));
  1170.  
  1171. static int line_for_error PROTO((int));
  1172. static int grow_outbuf PROTO((FILE_BUF *, int));
  1173.  
  1174. static HASHNODE *install PROTO((U_CHAR *, int, enum node_type, char *, int));
  1175. HASHNODE *lookup PROTO((U_CHAR *, int, int));
  1176. static void delete_macro PROTO((HASHNODE *));
  1177. static int hashf PROTO((U_CHAR *, int, int));
  1178.  
  1179. static void dump_single_macro PROTO((HASHNODE *, FILE *));
  1180. static void dump_all_macros PROTO((void));
  1181. static void dump_defn_1 PROTO((U_CHAR *, int, int, FILE *));
  1182. static void dump_arg_n PROTO((DEFINITION *, int, FILE *));
  1183.  
  1184. static void initialize_char_syntax PROTO((void));
  1185. static void initialize_builtins PROTO((FILE_BUF *, FILE_BUF *));
  1186.  
  1187. static void make_definition PROTO((char *, FILE_BUF *));
  1188. static void make_undef PROTO((char *, FILE_BUF *));
  1189.  
  1190. static void make_assertion PROTO((char *, char *));
  1191.  
  1192. static void append_include_chain PROTO((struct file_name_list *, struct file_name_list *));
  1193.  
  1194. static void deps_output PROTO((char *, int));
  1195.  
  1196. static void fatal PRINTF_PROTO_1((char *, ...)) __attribute__ ((noreturn));
  1197. void fancy_abort PROTO((void)) __attribute__ ((noreturn));
  1198. static void perror_with_name PROTO((char *));
  1199. static void pfatal_with_name PROTO((char *)) __attribute__ ((noreturn));
  1200. static void pipe_closed PROTO((int)) __attribute__ ((noreturn));
  1201.  
  1202. static void memory_full PROTO((void)) __attribute__ ((noreturn));
  1203. GENERIC_PTR xmalloc PROTO((size_t));
  1204. static GENERIC_PTR xrealloc PROTO((GENERIC_PTR, size_t));
  1205. static GENERIC_PTR xcalloc PROTO((size_t, size_t));
  1206. static char *savestring PROTO((char *));
  1207.  
  1208. static int file_size_and_mode PROTO((int, int *, long int *));
  1209. static void output_dots PROTO((FILE *, int));
  1210.  
  1211. /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME,
  1212.    retrying if necessary.  Return a negative value if an error occurs,
  1213.    otherwise return the actual number of bytes read,
  1214.    which must be LEN unless end-of-file was reached.  */
  1215.  
  1216. static int
  1217. safe_read (desc, ptr, len)
  1218.      int desc;
  1219.      char *ptr;
  1220.      int len;
  1221. {
  1222.   int left = len;
  1223.   while (left > 0) {
  1224.     int nchars = read (desc, ptr, left);
  1225.     if (nchars < 0)
  1226.       {
  1227. #ifdef EINTR
  1228.     if (errno == EINTR)
  1229.       continue;
  1230. #endif
  1231.     return nchars;
  1232.       }
  1233.     if (nchars == 0)
  1234.       break;
  1235.     ptr += nchars;
  1236.     left -= nchars;
  1237.   }
  1238.   return len - left;
  1239. }
  1240.  
  1241. /* Write LEN bytes at PTR to descriptor DESC,
  1242.    retrying if necessary, and treating any real error as fatal.  */
  1243.  
  1244. static void
  1245. safe_write (desc, ptr, len)
  1246.      int desc;
  1247.      char *ptr;
  1248.      int len;
  1249. {
  1250.   while (len > 0) {
  1251.     int written = write (desc, ptr, len);
  1252.     if (written < 0)
  1253.       {
  1254. #ifdef EINTR
  1255.     if (errno == EINTR)
  1256.       continue;
  1257. #endif
  1258.     pfatal_with_name (out_fname);
  1259.       }
  1260.     ptr += written;
  1261.     len -= written;
  1262.   }
  1263. }
  1264.  
  1265. int
  1266. main (argc, argv)
  1267.      int argc;
  1268.      char **argv;
  1269. {
  1270.   int st_mode;
  1271.   long st_size;
  1272.   char *in_fname;
  1273.   char *cp;
  1274.   int f, i;
  1275.   FILE_BUF *fp;
  1276.   char **pend_files = (char **) xmalloc (argc * sizeof (char *));
  1277.   char **pend_defs = (char **) xmalloc (argc * sizeof (char *));
  1278.   char **pend_undefs = (char **) xmalloc (argc * sizeof (char *));
  1279.   char **pend_assertions = (char **) xmalloc (argc * sizeof (char *));
  1280.   char **pend_includes = (char **) xmalloc (argc * sizeof (char *));
  1281.  
  1282.   /* Record the option used with each element of pend_assertions.
  1283.      This is preparation for supporting more than one option for making
  1284.      an assertion.  */
  1285.   char **pend_assertion_options = (char **) xmalloc (argc * sizeof (char *));
  1286.   int inhibit_predefs = 0;
  1287.   int no_standard_includes = 0;
  1288.   int no_standard_cplusplus_includes = 0;
  1289.   int missing_newline = 0;
  1290.  
  1291.   /* Non-0 means don't output the preprocessed program.  */
  1292.   int inhibit_output = 0;
  1293.   /* Non-0 means -v, so print the full set of include dirs.  */
  1294.   int verbose = 0;
  1295.  
  1296.   /* File name which deps are being written to.
  1297.      This is 0 if deps are being written to stdout.  */
  1298.   char *deps_file = 0;
  1299.   /* Fopen file mode to open deps_file with.  */
  1300.   char *deps_mode = "a";
  1301.   /* Stream on which to print the dependency information.  */
  1302.   FILE *deps_stream = 0;
  1303.   /* Target-name to write with the dependency information.  */
  1304.   char *deps_target = 0;
  1305.  
  1306. #ifdef RLIMIT_STACK
  1307.   /* Get rid of any avoidable limit on stack size.  */
  1308.   {
  1309.     struct rlimit rlim;
  1310.  
  1311.     /* Set the stack limit huge so that alloca (particularly stringtab
  1312.      * in dbxread.c) does not fail. */
  1313.     getrlimit (RLIMIT_STACK, &rlim);
  1314.     rlim.rlim_cur = rlim.rlim_max;
  1315.     setrlimit (RLIMIT_STACK, &rlim);
  1316.   }
  1317. #endif /* RLIMIT_STACK defined */
  1318.  
  1319. #ifdef SIGPIPE
  1320.   signal (SIGPIPE, pipe_closed);
  1321. #endif
  1322.  
  1323.   cp = argv[0] + strlen (argv[0]);
  1324.   while (cp != argv[0] && cp[-1] != '/'
  1325. #ifdef DIR_SEPARATOR
  1326.      && cp[-1] != DIR_SEPARATOR
  1327. #endif
  1328.      )
  1329.     --cp;
  1330.   progname = cp;
  1331.  
  1332. #ifdef VMS
  1333.   {
  1334.     /* Remove directories from PROGNAME.  */
  1335.     char *p;
  1336.     char *s = progname;
  1337.  
  1338.     if ((p = rindex (s, ':')) != 0) s = p + 1;    /* skip device */
  1339.     if ((p = rindex (s, ']')) != 0) s = p + 1;    /* skip directory */
  1340.     if ((p = rindex (s, '>')) != 0) s = p + 1;    /* alternate (int'n'l) dir */
  1341.     s = progname = savestring (s);
  1342.     if ((p = rindex (s, ';')) != 0) *p = '\0';    /* strip version number */
  1343.     if ((p = rindex (s, '.')) != 0        /* strip type iff ".exe" */
  1344.     && (p[1] == 'e' || p[1] == 'E')
  1345.     && (p[2] == 'x' || p[2] == 'X')
  1346.     && (p[3] == 'e' || p[3] == 'E')
  1347.     && !p[4])
  1348.       *p = '\0';
  1349.   }
  1350. #endif
  1351.  
  1352.   in_fname = NULL;
  1353.   out_fname = NULL;
  1354.  
  1355.   /* Initialize is_idchar to allow $.  */
  1356.   dollars_in_ident = 1;
  1357.   initialize_char_syntax ();
  1358.   dollars_in_ident = DOLLARS_IN_IDENTIFIERS > 0;
  1359.  
  1360.   no_line_directives = 0;
  1361.   no_trigraphs = 1;
  1362.   no_msdoscr = 0;
  1363.   dump_macros = dump_none;
  1364.   no_output = 0;
  1365.   cplusplus = 0;
  1366.   cplusplus_comments = 0;
  1367.  
  1368.   bzero ((char *) pend_files, argc * sizeof (char *));
  1369.   bzero ((char *) pend_defs, argc * sizeof (char *));
  1370.   bzero ((char *) pend_undefs, argc * sizeof (char *));
  1371.   bzero ((char *) pend_assertions, argc * sizeof (char *));
  1372.   bzero ((char *) pend_includes, argc * sizeof (char *));
  1373.  
  1374.   /* Process switches and find input file name.  */
  1375.  
  1376.   for (i = 1; i < argc; i++) {
  1377.     if (argv[i][0] != '-') {
  1378.       if (out_fname != NULL)
  1379.     fatal ("Usage: %s [switches] input output", argv[0]);
  1380.       else if (in_fname != NULL)
  1381.     out_fname = argv[i];
  1382.       else
  1383.     in_fname = argv[i];
  1384.     } else {
  1385.       switch (argv[i][1]) {
  1386.  
  1387.       case 'i':
  1388.     if (!strcmp (argv[i], "-include")) {
  1389.       if (i + 1 == argc)
  1390.         fatal ("Filename missing after `-include' option");
  1391.       else
  1392.         pend_includes[i] = argv[i+1], i++;
  1393.     }
  1394.     if (!strcmp (argv[i], "-imacros")) {
  1395.       if (i + 1 == argc)
  1396.         fatal ("Filename missing after `-imacros' option");
  1397.       else
  1398.         pend_files[i] = argv[i+1], i++;
  1399.     }
  1400.     if (!strcmp (argv[i], "-iprefix")) {
  1401.       if (i + 1 == argc)
  1402.         fatal ("Filename missing after `-iprefix' option");
  1403.       else
  1404.         include_prefix = argv[++i];
  1405.     }
  1406.     if (!strcmp (argv[i], "-ifoutput")) {
  1407.       output_conditionals = 1;
  1408.     }
  1409.     if (!strcmp (argv[i], "-imsdos")) {
  1410.       no_msdoscr = 1;
  1411.     }
  1412.     if (!strcmp (argv[i], "-isystem")) {
  1413.       struct file_name_list *dirtmp;
  1414.  
  1415.       if (i + 1 == argc)
  1416.         fatal ("Filename missing after `-isystem' option");
  1417.  
  1418.       dirtmp = (struct file_name_list *)
  1419.         xmalloc (sizeof (struct file_name_list));
  1420.       dirtmp->next = 0;
  1421.       dirtmp->control_macro = 0;
  1422.       dirtmp->c_system_include_path = 1;
  1423.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + 1);
  1424.       strcpy (dirtmp->fname, argv[++i]);
  1425.       dirtmp->got_name_map = 0;
  1426.  
  1427.       if (before_system == 0)
  1428.         before_system = dirtmp;
  1429.       else
  1430.         last_before_system->next = dirtmp;
  1431.       last_before_system = dirtmp; /* Tail follows the last one */
  1432.     }
  1433.     /* Add directory to end of path for includes,
  1434.        with the default prefix at the front of its name.  */
  1435.     if (!strcmp (argv[i], "-iwithprefix")) {
  1436.       struct file_name_list *dirtmp;
  1437.       char *prefix;
  1438.  
  1439.       if (include_prefix != 0)
  1440.         prefix = include_prefix;
  1441.       else {
  1442.         prefix = savestring (GCC_INCLUDE_DIR);
  1443.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1444.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1445.           prefix[strlen (prefix) - 7] = 0;
  1446.       }
  1447.  
  1448.       dirtmp = (struct file_name_list *)
  1449.         xmalloc (sizeof (struct file_name_list));
  1450.       dirtmp->next = 0;    /* New one goes on the end */
  1451.       dirtmp->control_macro = 0;
  1452.       dirtmp->c_system_include_path = 0;
  1453.       if (i + 1 == argc)
  1454.         fatal ("Directory name missing after `-iwithprefix' option");
  1455.  
  1456.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1);
  1457.       strcpy (dirtmp->fname, prefix);
  1458.       strcat (dirtmp->fname, argv[++i]);
  1459.       dirtmp->got_name_map = 0;
  1460.  
  1461.       if (after_include == 0)
  1462.         after_include = dirtmp;
  1463.       else
  1464.         last_after_include->next = dirtmp;
  1465.       last_after_include = dirtmp; /* Tail follows the last one */
  1466.     }
  1467.     /* Add directory to main path for includes,
  1468.        with the default prefix at the front of its name.  */
  1469.     if (!strcmp (argv[i], "-iwithprefixbefore")) {
  1470.       struct file_name_list *dirtmp;
  1471.       char *prefix;
  1472.  
  1473.       if (include_prefix != 0)
  1474.         prefix = include_prefix;
  1475.       else {
  1476.         prefix = savestring (GCC_INCLUDE_DIR);
  1477.         /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  1478.         if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
  1479.           prefix[strlen (prefix) - 7] = 0;
  1480.       }
  1481.  
  1482.       dirtmp = (struct file_name_list *)
  1483.         xmalloc (sizeof (struct file_name_list));
  1484.       dirtmp->next = 0;    /* New one goes on the end */
  1485.       dirtmp->control_macro = 0;
  1486.       dirtmp->c_system_include_path = 0;
  1487.       if (i + 1 == argc)
  1488.         fatal ("Directory name missing after `-iwithprefixbefore' option");
  1489.  
  1490.       dirtmp->fname = xmalloc (strlen (argv[i+1]) + strlen (prefix) + 1);
  1491.       strcpy (dirtmp->fname, prefix);
  1492.       strcat (dirtmp->fname, argv[++i]);
  1493.       dirtmp->got_name_map = 0;
  1494.  
  1495.       append_include_chain (dirtmp, dirtmp);
  1496.     }
  1497.     /* Add directory to end of path for includes.  */
  1498.     if (!strcmp (argv[i], "-idirafter")) {
  1499.       struct file_name_list *dirtmp;
  1500.  
  1501.       dirtmp = (struct file_name_list *)
  1502.         xmalloc (sizeof (struct file_name_list));
  1503.       dirtmp->next = 0;    /* New one goes on the end */
  1504.       dirtmp->control_macro = 0;
  1505.       dirtmp->c_system_include_path = 0;
  1506.       if (i + 1 == argc)
  1507.         fatal ("Directory name missing after `-idirafter' option");
  1508.       else
  1509.         dirtmp->fname = argv[++i];
  1510.       dirtmp->got_name_map = 0;
  1511.  
  1512.       if (after_include == 0)
  1513.         after_include = dirtmp;
  1514.       else
  1515.         last_after_include->next = dirtmp;
  1516.       last_after_include = dirtmp; /* Tail follows the last one */
  1517.     }
  1518.     break;
  1519.  
  1520.       case 'o':
  1521.     if (out_fname != NULL)
  1522.       fatal ("Output filename specified twice");
  1523.     if (i + 1 == argc)
  1524.       fatal ("Filename missing after -o option");
  1525.     out_fname = argv[++i];
  1526.     if (!strcmp (out_fname, "-"))
  1527.       out_fname = "";
  1528.     break;
  1529.  
  1530.       case 'p':
  1531.     if (!strcmp (argv[i], "-pedantic"))
  1532.       pedantic = 1;
  1533.     else if (!strcmp (argv[i], "-pedantic-errors")) {
  1534.       pedantic = 1;
  1535.       pedantic_errors = 1;
  1536.     } else if (!strcmp (argv[i], "-pcp")) {
  1537.       char *pcp_fname;
  1538.       if (i + 1 == argc)
  1539.         fatal ("Filename missing after -pcp option");
  1540.       pcp_fname = argv[++i];
  1541.       pcp_outfile = 
  1542.         ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
  1543.          ? fopen (pcp_fname, "w")
  1544.          : stdout);
  1545.       if (pcp_outfile == 0)
  1546.         pfatal_with_name (pcp_fname);
  1547.       no_precomp = 1;
  1548.     }
  1549.     break;
  1550.  
  1551.       case 't':
  1552.     if (!strcmp (argv[i], "-traditional")) {
  1553.       traditional = 1;
  1554.       if (dollars_in_ident > 0)
  1555.         dollars_in_ident = 1;
  1556.     } else if (!strcmp (argv[i], "-trigraphs")) {
  1557.       no_trigraphs = 0;
  1558.     }
  1559.     break;
  1560.  
  1561.       case 'l':
  1562.     if (! strcmp (argv[i], "-lang-c"))
  1563.       cplusplus = 0, cplusplus_comments = 0, objc = 0;
  1564.     if (! strcmp (argv[i], "-lang-c++"))
  1565.       cplusplus = 1, cplusplus_comments = 1, objc = 0;
  1566.     if (! strcmp (argv[i], "-lang-c-c++-comments"))
  1567.       cplusplus = 0, cplusplus_comments = 1, objc = 0;
  1568.     if (! strcmp (argv[i], "-lang-objc"))
  1569.       objc = 1, cplusplus = 0, cplusplus_comments = 1;
  1570.     if (! strcmp (argv[i], "-lang-objc++"))
  1571.       objc = 1, cplusplus = 1, cplusplus_comments = 1;
  1572.      if (! strcmp (argv[i], "-lang-asm"))
  1573.        lang_asm = 1;
  1574.      if (! strcmp (argv[i], "-lint"))
  1575.        for_lint = 1;
  1576.     break;
  1577.  
  1578.       case '+':
  1579.     cplusplus = 1, cplusplus_comments = 1;
  1580.     break;
  1581.  
  1582.       case 'w':
  1583.     inhibit_warnings = 1;
  1584.     break;
  1585.  
  1586.       case 'W':
  1587.     if (!strcmp (argv[i], "-Wtrigraphs"))
  1588.       warn_trigraphs = 1;
  1589.     else if (!strcmp (argv[i], "-Wno-trigraphs"))
  1590.       warn_trigraphs = 0;
  1591.     else if (!strcmp (argv[i], "-Wcomment"))
  1592.       warn_comments = 1;
  1593.     else if (!strcmp (argv[i], "-Wno-comment"))
  1594.       warn_comments = 0;
  1595.     else if (!strcmp (argv[i], "-Wcomments"))
  1596.       warn_comments = 1;
  1597.     else if (!strcmp (argv[i], "-Wno-comments"))
  1598.       warn_comments = 0;
  1599.     else if (!strcmp (argv[i], "-Wtraditional"))
  1600.       warn_stringify = 1;
  1601.     else if (!strcmp (argv[i], "-Wno-traditional"))
  1602.       warn_stringify = 0;
  1603.     else if (!strcmp (argv[i], "-Wimport"))
  1604.       warn_import = 1;
  1605.     else if (!strcmp (argv[i], "-Wno-import"))
  1606.       warn_import = 0;
  1607.     else if (!strcmp (argv[i], "-Werror"))
  1608.       warnings_are_errors = 1;
  1609.     else if (!strcmp (argv[i], "-Wno-error"))
  1610.       warnings_are_errors = 0;
  1611.     else if (!strcmp (argv[i], "-Wall"))
  1612.       {
  1613.         warn_trigraphs = 1;
  1614.         warn_comments = 1;
  1615.       }
  1616.     break;
  1617.  
  1618.       case 'M':
  1619.     /* The style of the choices here is a bit mixed.
  1620.        The chosen scheme is a hybrid of keeping all options in one string
  1621.        and specifying each option in a separate argument:
  1622.        -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
  1623.        -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
  1624.        -M[M][G][D file].  This is awkward to handle in specs, and is not
  1625.        as extensible.  */
  1626.     /* ??? -MG must be specified in addition to one of -M or -MM.
  1627.        This can be relaxed in the future without breaking anything.
  1628.        The converse isn't true.  */
  1629.  
  1630.     /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
  1631.     if (!strcmp (argv[i], "-MG"))
  1632.       {
  1633.         print_deps_missing_files = 1;
  1634.         break;
  1635.       }
  1636.     if (!strcmp (argv[i], "-M"))
  1637.       print_deps = 2;
  1638.     else if (!strcmp (argv[i], "-MM"))
  1639.       print_deps = 1;
  1640.     else if (!strcmp (argv[i], "-MD"))
  1641.       print_deps = 2;
  1642.     else if (!strcmp (argv[i], "-MMD"))
  1643.       print_deps = 1;
  1644.     /* For -MD and -MMD options, write deps on file named by next arg.  */
  1645.     if (!strcmp (argv[i], "-MD")
  1646.         || !strcmp (argv[i], "-MMD")) {
  1647.       if (i + 1 == argc)
  1648.         fatal ("Filename missing after %s option", argv[i]);
  1649.       i++;
  1650.       deps_file = argv[i];
  1651.       deps_mode = "w";
  1652.     } else {
  1653.       /* For -M and -MM, write deps on standard output
  1654.          and suppress the usual output.  */
  1655.       deps_stream = stdout;
  1656.       inhibit_output = 1;
  1657.     }      
  1658.     break;
  1659.  
  1660.       case 'd':
  1661.     {
  1662.       char *p = argv[i] + 2;
  1663.       char c;
  1664.       while ((c = *p++)) {
  1665.         /* Arg to -d specifies what parts of macros to dump */
  1666.         switch (c) {
  1667.         case 'M':
  1668.           dump_macros = dump_only;
  1669.           no_output = 1;
  1670.           break;
  1671.         case 'N':
  1672.           dump_macros = dump_names;
  1673.           break;
  1674.         case 'D':
  1675.           dump_macros = dump_definitions;
  1676.           break;
  1677.         }
  1678.       }
  1679.     }
  1680.     break;
  1681.  
  1682.       case 'g':
  1683.     if (argv[i][2] == '3')
  1684.       debug_output = 1;
  1685.     break;
  1686.  
  1687.       case 'v':
  1688.     fprintf (stderr, "GNU CPP version %s", version_string);
  1689. #ifdef TARGET_VERSION
  1690.     TARGET_VERSION;
  1691. #endif
  1692.     fprintf (stderr, "\n");
  1693.     verbose = 1;
  1694.     break;
  1695.  
  1696.       case 'H':
  1697.     print_include_names = 1;
  1698.     break;
  1699.  
  1700.       case 'D':
  1701.     if (argv[i][2] != 0)
  1702.       pend_defs[i] = argv[i] + 2;
  1703.     else if (i + 1 == argc)
  1704.       fatal ("Macro name missing after -D option");
  1705.     else
  1706.       i++, pend_defs[i] = argv[i];
  1707.     break;
  1708.  
  1709.       case 'A':
  1710.     {
  1711.       char *p;
  1712.  
  1713.       if (argv[i][2] != 0)
  1714.         p = argv[i] + 2;
  1715.       else if (i + 1 == argc)
  1716.         fatal ("Assertion missing after -A option");
  1717.       else
  1718.         p = argv[++i];
  1719.  
  1720.       if (!strcmp (p, "-")) {
  1721.         /* -A- eliminates all predefined macros and assertions.
  1722.            Let's include also any that were specified earlier
  1723.            on the command line.  That way we can get rid of any
  1724.            that were passed automatically in from GCC.  */
  1725.         int j;
  1726.         inhibit_predefs = 1;
  1727.         for (j = 0; j < i; j++)
  1728.           pend_defs[j] = pend_assertions[j] = 0;
  1729.       } else {
  1730.         pend_assertions[i] = p;
  1731.         pend_assertion_options[i] = "-A";
  1732.       }
  1733.     }
  1734.     break;
  1735.  
  1736.       case 'U':        /* JF #undef something */
  1737.     if (argv[i][2] != 0)
  1738.       pend_undefs[i] = argv[i] + 2;
  1739.     else if (i + 1 == argc)
  1740.       fatal ("Macro name missing after -U option");
  1741.     else
  1742.       pend_undefs[i] = argv[i+1], i++;
  1743.     break;
  1744.  
  1745.       case 'C':
  1746.     put_out_comments = 1;
  1747.     break;
  1748.  
  1749.       case 'E':            /* -E comes from cc -E; ignore it.  */
  1750.     break;
  1751.  
  1752.       case 'P':
  1753.     no_line_directives = 1;
  1754.     break;
  1755.  
  1756.       case '$':            /* Don't include $ in identifiers.  */
  1757.     dollars_in_ident = 0;
  1758.     break;
  1759.  
  1760.       case 'I':            /* Add directory to path for includes.  */
  1761.     {
  1762.       struct file_name_list *dirtmp;
  1763.  
  1764.       if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) {
  1765.         ignore_srcdir = 1;
  1766.         /* Don't use any preceding -I directories for #include <...>.  */
  1767.         first_bracket_include = 0;
  1768.       }
  1769.       else {
  1770.         dirtmp = (struct file_name_list *)
  1771.           xmalloc (sizeof (struct file_name_list));
  1772.         dirtmp->next = 0;        /* New one goes on the end */
  1773.         dirtmp->control_macro = 0;
  1774.         dirtmp->c_system_include_path = 0;
  1775.         if (argv[i][2] != 0)
  1776.           dirtmp->fname = argv[i] + 2;
  1777.         else if (i + 1 == argc)
  1778.           fatal ("Directory name missing after -I option");
  1779.         else
  1780.           dirtmp->fname = argv[++i];
  1781.         dirtmp->got_name_map = 0;
  1782.         append_include_chain (dirtmp, dirtmp);
  1783.       }
  1784.     }
  1785.     break;
  1786.  
  1787.       case 'n':
  1788.     if (!strcmp (argv[i], "-nostdinc"))
  1789.       /* -nostdinc causes no default include directories.
  1790.          You must specify all include-file directories with -I.  */
  1791.       no_standard_includes = 1;
  1792.     else if (!strcmp (argv[i], "-nostdinc++"))
  1793.       /* -nostdinc++ causes no default C++-specific include directories. */
  1794.       no_standard_cplusplus_includes = 1;
  1795.     else if (!strcmp (argv[i], "-noprecomp"))
  1796.       no_precomp = 1;
  1797.     break;
  1798.  
  1799.       case 'u':
  1800.     /* Sun compiler passes undocumented switch "-undef".
  1801.        Let's assume it means to inhibit the predefined symbols.  */
  1802.     inhibit_predefs = 1;
  1803.     break;
  1804.  
  1805.       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
  1806.     if (in_fname == NULL) {
  1807.       in_fname = "";
  1808.       break;
  1809.     } else if (out_fname == NULL) {
  1810.       out_fname = "";
  1811.       break;
  1812.     }    /* else fall through into error */
  1813.  
  1814.       default:
  1815.     fatal ("Invalid option `%s'", argv[i]);
  1816.       }
  1817.     }
  1818.   }
  1819.  
  1820.   /* Add dirs from CPATH after dirs from -I.  */
  1821.   /* There seems to be confusion about what CPATH should do,
  1822.      so for the moment it is not documented.  */
  1823.   /* Some people say that CPATH should replace the standard include dirs,
  1824.      but that seems pointless: it comes before them, so it overrides them
  1825.      anyway.  */
  1826.   cp = getenv ("CPATH");
  1827.   if (cp && ! no_standard_includes)
  1828.     path_include (cp);
  1829.  
  1830.   /* Now that dollars_in_ident is known, initialize is_idchar.  */
  1831.   initialize_char_syntax ();
  1832.  
  1833.   /* Initialize output buffer */
  1834.  
  1835.   outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
  1836.   outbuf.bufp = outbuf.buf;
  1837.   outbuf.length = OUTBUF_SIZE;
  1838.  
  1839.   /* Do partial setup of input buffer for the sake of generating
  1840.      early #line directives (when -g is in effect).  */
  1841.  
  1842.   fp = &instack[++indepth];
  1843.   if (in_fname == NULL)
  1844.     in_fname = "";
  1845.   fp->nominal_fname = fp->fname = in_fname;
  1846.   fp->lineno = 0;
  1847.  
  1848.   /* In C++, wchar_t is a distinct basic type, and we can expect
  1849.      __wchar_t to be defined by cc1plus.  */
  1850.   if (cplusplus)
  1851.     wchar_type = "__wchar_t";
  1852.  
  1853.   /* Install __LINE__, etc.  Must follow initialize_char_syntax
  1854.      and option processing.  */
  1855.   initialize_builtins (fp, &outbuf);
  1856.  
  1857.   /* Do standard #defines and assertions
  1858.      that identify system and machine type.  */
  1859.  
  1860.   if (!inhibit_predefs) {
  1861.     char *p = (char *) alloca (strlen (predefs) + 1);
  1862.     strcpy (p, predefs);
  1863.     while (*p) {
  1864.       char *q;
  1865.       while (*p == ' ' || *p == '\t')
  1866.     p++;
  1867.       /* Handle -D options.  */ 
  1868.       if (p[0] == '-' && p[1] == 'D') {
  1869.     q = &p[2];
  1870.     while (*p && *p != ' ' && *p != '\t')
  1871.       p++;
  1872.     if (*p != 0)
  1873.       *p++= 0;
  1874.     if (debug_output)
  1875.       output_line_directive (fp, &outbuf, 0, same_file);
  1876.     make_definition (q, &outbuf);
  1877.     while (*p == ' ' || *p == '\t')
  1878.       p++;
  1879.       } else if (p[0] == '-' && p[1] == 'A') {
  1880.     /* Handle -A options (assertions).  */ 
  1881.     char *assertion;
  1882.     char *past_name;
  1883.     char *value;
  1884.     char *past_value;
  1885.     char *termination;
  1886.     int save_char;
  1887.  
  1888.     assertion = &p[2];
  1889.     past_name = assertion;
  1890.     /* Locate end of name.  */
  1891.     while (*past_name && *past_name != ' '
  1892.            && *past_name != '\t' && *past_name != '(')
  1893.       past_name++;
  1894.     /* Locate `(' at start of value.  */
  1895.     value = past_name;
  1896.     while (*value && (*value == ' ' || *value == '\t'))
  1897.       value++;
  1898.     if (*value++ != '(')
  1899.       abort ();
  1900.     while (*value && (*value == ' ' || *value == '\t'))
  1901.       value++;
  1902.     past_value = value;
  1903.     /* Locate end of value.  */
  1904.     while (*past_value && *past_value != ' '
  1905.            && *past_value != '\t' && *past_value != ')')
  1906.       past_value++;
  1907.     termination = past_value;
  1908.     while (*termination && (*termination == ' ' || *termination == '\t'))
  1909.       termination++;
  1910.     if (*termination++ != ')')
  1911.       abort ();
  1912.     if (*termination && *termination != ' ' && *termination != '\t')
  1913.       abort ();
  1914.     /* Temporarily null-terminate the value.  */
  1915.     save_char = *termination;
  1916.     *termination = '\0';
  1917.     /* Install the assertion.  */
  1918.     make_assertion ("-A", assertion);
  1919.     *termination = (char) save_char;
  1920.     p = termination;
  1921.     while (*p == ' ' || *p == '\t')
  1922.       p++;
  1923.       } else {
  1924.     abort ();
  1925.       }
  1926.     }
  1927.   }
  1928.  
  1929.   /* Now handle the command line options.  */
  1930.  
  1931.   /* Do -U's, -D's and -A's in the order they were seen.  */
  1932.   for (i = 1; i < argc; i++) {
  1933.     if (pend_undefs[i]) {
  1934.       if (debug_output)
  1935.         output_line_directive (fp, &outbuf, 0, same_file);
  1936.       make_undef (pend_undefs[i], &outbuf);
  1937.     }
  1938.     if (pend_defs[i]) {
  1939.       if (debug_output)
  1940.         output_line_directive (fp, &outbuf, 0, same_file);
  1941.       make_definition (pend_defs[i], &outbuf);
  1942.     }
  1943.     if (pend_assertions[i])
  1944.       make_assertion (pend_assertion_options[i], pend_assertions[i]);
  1945.   }
  1946.  
  1947.   done_initializing = 1;
  1948.  
  1949.   { /* read the appropriate environment variable and if it exists
  1950.        replace include_defaults with the listed path. */
  1951.     char *epath = 0;
  1952.     switch ((objc << 1) + cplusplus)
  1953.       {
  1954.       case 0:
  1955.     epath = getenv ("C_INCLUDE_PATH");
  1956.     break;
  1957.       case 1:
  1958.     epath = getenv ("CPLUS_INCLUDE_PATH");
  1959.     break;
  1960.       case 2:
  1961.     epath = getenv ("OBJC_INCLUDE_PATH");
  1962.     break;
  1963.       case 3:
  1964.     epath = getenv ("OBJCPLUS_INCLUDE_PATH");
  1965.     break;
  1966.       }
  1967.     /* If the environment var for this language is set,
  1968.        add to the default list of include directories.  */
  1969.     if (epath) {
  1970.       char *nstore = (char *) alloca (strlen (epath) + 2);
  1971.       int num_dirs;
  1972.       char *startp, *endp;
  1973.  
  1974.       for (num_dirs = 1, startp = epath; *startp; startp++)
  1975.     if (*startp == PATH_SEPARATOR)
  1976.       num_dirs++;
  1977.       include_defaults
  1978.     = (struct default_include *) xmalloc ((num_dirs
  1979.                            * sizeof (struct default_include))
  1980.                           + sizeof (include_defaults_array));
  1981.       startp = endp = epath;
  1982.       num_dirs = 0;
  1983.       while (1) {
  1984.         /* Handle cases like c:/usr/lib:d:/gcc/lib */
  1985.         if ((*endp == PATH_SEPARATOR
  1986. #if 0 /* Obsolete, now that we use semicolons as the path separator.  */
  1987. #ifdef __MSDOS__
  1988.          && (endp-startp != 1 || !isalpha (*startp))
  1989. #endif
  1990. #endif
  1991.          )
  1992.             || *endp == 0) {
  1993.       strncpy (nstore, startp, endp-startp);
  1994.       if (endp == startp)
  1995.         strcpy (nstore, ".");
  1996.       else
  1997.         nstore[endp-startp] = '\0';
  1998.  
  1999.       include_defaults[num_dirs].fname = savestring (nstore);
  2000.       include_defaults[num_dirs].cplusplus = cplusplus;
  2001.       include_defaults[num_dirs].cxx_aware = 1;
  2002.       num_dirs++;
  2003.       if (*endp == '\0')
  2004.         break;
  2005.       endp = startp = endp + 1;
  2006.     } else
  2007.       endp++;
  2008.       }
  2009.       /* Put the usual defaults back in at the end.  */
  2010.       bcopy ((char *) include_defaults_array,
  2011.          (char *) &include_defaults[num_dirs],
  2012.          sizeof (include_defaults_array));
  2013.     }
  2014.   }
  2015.  
  2016.   append_include_chain (before_system, last_before_system);
  2017.   first_system_include = before_system;
  2018.  
  2019.   /* Unless -fnostdinc,
  2020.      tack on the standard include file dirs to the specified list */
  2021.   if (!no_standard_includes) {
  2022.     struct default_include *p = include_defaults;
  2023.     char *specd_prefix = include_prefix;
  2024.     char *default_prefix = savestring (GCC_INCLUDE_DIR);
  2025.     int default_len = 0;
  2026.     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
  2027.     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
  2028.       default_len = strlen (default_prefix) - 7;
  2029.       default_prefix[default_len] = 0;
  2030.     }
  2031.     /* Search "translated" versions of GNU directories.
  2032.        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
  2033.     if (specd_prefix != 0 && default_len != 0)
  2034.       for (p = include_defaults; p->fname; p++) {
  2035.     /* Some standard dirs are only for C++.  */
  2036.     if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2037.       /* Does this dir start with the prefix?  */
  2038.       if (!strncmp (p->fname, default_prefix, default_len)) {
  2039.         /* Yes; change prefix and add to search list.  */
  2040.         struct file_name_list *new
  2041.           = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2042.         int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
  2043.         char *str = xmalloc (this_len + 1);
  2044.         strcpy (str, specd_prefix);
  2045.         strcat (str, p->fname + default_len);
  2046.         new->fname = str;
  2047.         new->control_macro = 0;
  2048.         new->c_system_include_path = !p->cxx_aware;
  2049.         new->got_name_map = 0;
  2050.         append_include_chain (new, new);
  2051.         if (first_system_include == 0)
  2052.           first_system_include = new;
  2053.       }
  2054.     }
  2055.       }
  2056.     /* Search ordinary names for GNU include directories.  */
  2057.     for (p = include_defaults; p->fname; p++) {
  2058.       /* Some standard dirs are only for C++.  */
  2059.       if (!p->cplusplus || (cplusplus && !no_standard_cplusplus_includes)) {
  2060.     struct file_name_list *new
  2061.       = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  2062.     new->control_macro = 0;
  2063.     new->c_system_include_path = !p->cxx_aware;
  2064.     new->fname = p->fname;
  2065.     new->got_name_map = 0;
  2066.     append_include_chain (new, new);
  2067.     if (first_system_include == 0)
  2068.       first_system_include = new;
  2069.       }
  2070.     }
  2071.   }
  2072.  
  2073.   /* Tack the after_include chain at the end of the include chain.  */
  2074.   append_include_chain (after_include, last_after_include);
  2075.   if (first_system_include == 0)
  2076.     first_system_include = after_include;
  2077.  
  2078.   /* With -v, print the list of dirs to search.  */
  2079.   if (verbose) {
  2080.     struct file_name_list *p;
  2081.     fprintf (stderr, "#include \"...\" search starts here:\n");
  2082.     for (p = include; p; p = p->next) {
  2083.       if (p == first_bracket_include)
  2084.     fprintf (stderr, "#include <...> search starts here:\n");
  2085.       fprintf (stderr, " %s\n", p->fname);
  2086.     }
  2087.     fprintf (stderr, "End of search list.\n");
  2088.   }
  2089.  
  2090.   /* Scan the -imacros files before the main input.
  2091.      Much like #including them, but with no_output set
  2092.      so that only their macro definitions matter.  */
  2093.  
  2094.   no_output++; no_record_file++;
  2095.   for (i = 1; i < argc; i++)
  2096.     if (pend_files[i]) {
  2097.       int fd = open (pend_files[i], O_RDONLY, 0666);
  2098.       if (fd < 0) {
  2099.     perror_with_name (pend_files[i]);
  2100.     return FATAL_EXIT_CODE;
  2101.       }
  2102.       finclude (fd, pend_files[i], &outbuf, 0, NULL_PTR);
  2103.     }
  2104.   no_output--; no_record_file--;
  2105.  
  2106.   /* Copy the entire contents of the main input file into
  2107.      the stacked input buffer previously allocated for it.  */
  2108.  
  2109.   /* JF check for stdin */
  2110.   if (in_fname == NULL || *in_fname == 0) {
  2111.     in_fname = "";
  2112.     f = 0;
  2113.   } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
  2114.     goto perror;
  2115.  
  2116.   /* -MG doesn't select the form of output and must be specified with one of
  2117.      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
  2118.      inhibit compilation.  */
  2119.   if (print_deps_missing_files && (print_deps == 0 || !inhibit_output))
  2120.     fatal ("-MG must be specified with one of -M or -MM");
  2121.  
  2122.   /* Either of two environment variables can specify output of deps.
  2123.      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
  2124.      where OUTPUT_FILE is the file to write deps info to
  2125.      and DEPS_TARGET is the target to mention in the deps.  */
  2126.  
  2127.   if (print_deps == 0
  2128.       && (getenv ("SUNPRO_DEPENDENCIES") != 0
  2129.       || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
  2130.     char *spec = getenv ("DEPENDENCIES_OUTPUT");
  2131.     char *s;
  2132.     char *output_file;
  2133.  
  2134.     if (spec == 0) {
  2135.       spec = getenv ("SUNPRO_DEPENDENCIES");
  2136.       print_deps = 2;
  2137.     }
  2138.     else
  2139.       print_deps = 1;
  2140.  
  2141.     s = spec;
  2142.     /* Find the space before the DEPS_TARGET, if there is one.  */
  2143.     /* This should use index.  (mrs) */
  2144.     while (*s != 0 && *s != ' ') s++;
  2145.     if (*s != 0) {
  2146.       deps_target = s + 1;
  2147.       output_file = xmalloc (s - spec + 1);
  2148.       bcopy (spec, output_file, s - spec);
  2149.       output_file[s - spec] = 0;
  2150.     }
  2151.     else {
  2152.       deps_target = 0;
  2153.       output_file = spec;
  2154.     }
  2155.       
  2156.     deps_file = output_file;
  2157.     deps_mode = "a";
  2158.   }
  2159.  
  2160.   /* For -M, print the expected object file name
  2161.      as the target of this Make-rule.  */
  2162.   if (print_deps) {
  2163.     deps_allocated_size = 200;
  2164.     deps_buffer = xmalloc (deps_allocated_size);
  2165.     deps_buffer[0] = 0;
  2166.     deps_size = 0;
  2167.     deps_column = 0;
  2168.  
  2169.     if (deps_target) {
  2170.       deps_output (deps_target, ':');
  2171.     } else if (*in_fname == 0) {
  2172.       deps_output ("-", ':');
  2173.     } else {
  2174.       char *p, *q;
  2175.       int len;
  2176.  
  2177.       /* Discard all directory prefixes from filename.  */
  2178.       if ((q = rindex (in_fname, '/')) != NULL
  2179. #ifdef DIR_SEPARATOR
  2180.       && (q = rindex (in_fname, DIR_SEPARATOR)) != NULL
  2181. #endif
  2182.       )
  2183.     ++q;
  2184.       else
  2185.     q = in_fname;
  2186.  
  2187.       /* Copy remainder to mungable area.  */
  2188.       p = (char *) alloca (strlen(q) + 8);
  2189.       strcpy (p, q);
  2190.  
  2191.       /* Output P, but remove known suffixes.  */
  2192.       len = strlen (p);
  2193.       q = p + len;
  2194.       if (len >= 2
  2195.       && p[len - 2] == '.'
  2196.       && index("cCsSm", p[len - 1]))
  2197.     q = p + (len - 2);
  2198.       else if (len >= 3
  2199.            && p[len - 3] == '.'
  2200.            && p[len - 2] == 'c'
  2201.            && p[len - 1] == 'c')
  2202.     q = p + (len - 3);
  2203.       else if (len >= 4
  2204.            && p[len - 4] == '.'
  2205.            && p[len - 3] == 'c'
  2206.            && p[len - 2] == 'x'
  2207.            && p[len - 1] == 'x')
  2208.     q = p + (len - 4);
  2209.       else if (len >= 4
  2210.            && p[len - 4] == '.'
  2211.            && p[len - 3] == 'c'
  2212.            && p[len - 2] == 'p'
  2213.            && p[len - 1] == 'p')
  2214.     q = p + (len - 4);
  2215.  
  2216.       /* Supply our own suffix.  */
  2217. #ifndef VMS
  2218.       strcpy (q, ".o");
  2219. #else
  2220.       strcpy (q, ".obj");
  2221. #endif
  2222.  
  2223.       deps_output (p, ':');
  2224.       deps_output (in_fname, ' ');
  2225.     }
  2226.   }
  2227.  
  2228.   file_size_and_mode (f, &st_mode, &st_size);
  2229.   fp->nominal_fname = fp->fname = in_fname;
  2230.   fp->lineno = 1;
  2231.   fp->system_header_p = 0;
  2232.   /* JF all this is mine about reading pipes and ttys */
  2233.   if (! S_ISREG (st_mode)) {
  2234.     /* Read input from a file that is not a normal disk file.
  2235.        We cannot preallocate a buffer with the correct size,
  2236.        so we must read in the file a piece at the time and make it bigger.  */
  2237.     int size;
  2238.     int bsize;
  2239.     int cnt;
  2240.  
  2241.     bsize = 2000;
  2242.     size = 0;
  2243.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  2244.     for (;;) {
  2245.       cnt = safe_read (f, (char *) fp->buf + size, bsize - size);
  2246.       if (cnt < 0) goto perror;    /* error! */
  2247.       size += cnt;
  2248.       if (size != bsize) break;    /* End of file */
  2249.       bsize *= 2;
  2250.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  2251.     }
  2252.     fp->length = size;
  2253.   } else {
  2254.     /* Read a file whose size we can determine in advance.
  2255.        For the sake of VMS, st_size is just an upper bound.  */
  2256.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  2257.     fp->length = safe_read (f, (char *) fp->buf, st_size);
  2258.     if (fp->length < 0) goto perror;
  2259.   }
  2260.   fp->bufp = fp->buf;
  2261.   fp->if_stack = if_stack;
  2262.  
  2263.   /* Make sure data ends with a newline.  And put a null after it.  */
  2264.  
  2265.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  2266.       /* Backslash-newline at end is not good enough.  */
  2267.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  2268.     fp->buf[fp->length++] = '\n';
  2269.     missing_newline = 1;
  2270.   }
  2271.   fp->buf[fp->length] = '\0';
  2272.  
  2273.   /* Convert MSDOS files */
  2274.   
  2275.   if (no_msdoscr)
  2276.     msdos_pcp (fp);
  2277.  
  2278.   /* Unless inhibited, convert trigraphs in the input.  */
  2279.  
  2280.   if (!no_trigraphs)
  2281.     trigraph_pcp (fp);
  2282.  
  2283.   /* Now that we know the input file is valid, open the output.  */
  2284.  
  2285.   if (!out_fname || !strcmp (out_fname, ""))
  2286.     out_fname = "stdout";
  2287.   else if (! freopen (out_fname, "w", stdout))
  2288.     pfatal_with_name (out_fname);
  2289.  
  2290.   output_line_directive (fp, &outbuf, 0, same_file);
  2291.  
  2292.   /* Scan the -include files before the main input.  */
  2293.  
  2294.   no_record_file++;
  2295.   for (i = 1; i < argc; i++)
  2296.     if (pend_includes[i]) {
  2297.       int fd = open (pend_includes[i], O_RDONLY, 0666);
  2298.       if (fd < 0) {
  2299.     perror_with_name (pend_includes[i]);
  2300.     return FATAL_EXIT_CODE;
  2301.       }
  2302.       finclude (fd, pend_includes[i], &outbuf, 0, NULL_PTR);
  2303.     }
  2304.   no_record_file--;
  2305.  
  2306.   /* Scan the input, processing macros and directives.  */
  2307.  
  2308.   rescan (&outbuf, 0);
  2309.  
  2310.   if (missing_newline)
  2311.     fp->lineno--;
  2312.  
  2313.   if (pedantic && missing_newline)
  2314.     pedwarn ("file does not end in newline");
  2315.  
  2316.   /* Now we have processed the entire input
  2317.      Write whichever kind of output has been requested.  */
  2318.  
  2319.   if (dump_macros == dump_only)
  2320.     dump_all_macros ();
  2321.   else if (! inhibit_output) {
  2322.     write_output ();
  2323.   }
  2324.  
  2325.   if (print_deps) {
  2326.     /* Don't actually write the deps file if compilation has failed.  */
  2327.     if (errors == 0) {
  2328.       if (deps_file && ! (deps_stream = fopen (deps_file, deps_mode)))
  2329.     pfatal_with_name (deps_file);
  2330.       fputs (deps_buffer, deps_stream);
  2331.       putc ('\n', deps_stream);
  2332.       if (deps_file) {
  2333.     if (ferror (deps_stream) || fclose (deps_stream) != 0)
  2334.       fatal ("I/O error on output");
  2335.       }
  2336.     }
  2337.   }
  2338.  
  2339.   if (pcp_outfile && pcp_outfile != stdout
  2340.       && (ferror (pcp_outfile) || fclose (pcp_outfile) != 0))
  2341.     fatal ("I/O error on `-pcp' output");
  2342.  
  2343.   if (ferror (stdout) || fclose (stdout) != 0)
  2344.     fatal ("I/O error on output");
  2345.  
  2346.   if (errors)
  2347.     exit (FATAL_EXIT_CODE);
  2348.   exit (SUCCESS_EXIT_CODE);
  2349.  
  2350.  perror:
  2351.   pfatal_with_name (in_fname);
  2352.   return 0;
  2353. }
  2354.  
  2355. /* Given a colon-separated list of file names PATH,
  2356.    add all the names to the search path for include files.  */
  2357.  
  2358. static void
  2359. path_include (path)
  2360.      char *path;
  2361. {
  2362.   char *p;
  2363.  
  2364.   p = path;
  2365.  
  2366.   if (*p)
  2367.     while (1) {
  2368.       char *q = p;
  2369.       char *name;
  2370.       struct file_name_list *dirtmp;
  2371.  
  2372.       /* Find the end of this name.  */
  2373.       while (*q != 0 && *q != PATH_SEPARATOR) q++;
  2374.       if (p == q) {
  2375.     /* An empty name in the path stands for the current directory.  */
  2376.     name = xmalloc (2);
  2377.     name[0] = '.';
  2378.     name[1] = 0;
  2379.       } else {
  2380.     /* Otherwise use the directory that is named.  */
  2381.     name = xmalloc (q - p + 1);
  2382.     bcopy (p, name, q - p);
  2383.     name[q - p] = 0;
  2384.       }
  2385.  
  2386.       dirtmp = (struct file_name_list *)
  2387.     xmalloc (sizeof (struct file_name_list));
  2388.       dirtmp->next = 0;        /* New one goes on the end */
  2389.       dirtmp->control_macro = 0;
  2390.       dirtmp->c_system_include_path = 0;
  2391.       dirtmp->fname = name;
  2392.       dirtmp->got_name_map = 0;
  2393.       append_include_chain (dirtmp, dirtmp);
  2394.  
  2395.       /* Advance past this name.  */
  2396.       p = q;
  2397.       if (*p == 0)
  2398.     break;
  2399.       /* Skip the colon.  */
  2400.       p++;
  2401.     }
  2402. }
  2403.  
  2404. /* Return the address of the first character in S that equals C.
  2405.    S is an array of length N, possibly containing '\0's, and followed by '\0'.
  2406.    Return 0 if there is no such character.  Assume that C itself is not '\0'.
  2407.    If we knew we could use memchr, we could just invoke memchr (S, C, N),
  2408.    but unfortunately memchr isn't autoconfigured yet.  */
  2409.  
  2410. static U_CHAR *
  2411. index0 (s, c, n)
  2412.      U_CHAR *s;
  2413.      int c;
  2414.      size_t n;
  2415. {
  2416.   char *p = (char *) s;
  2417.   for (;;) {
  2418.     char *q = index (p, c);
  2419.     if (q)
  2420.       return (U_CHAR *) q;
  2421.     else {
  2422.       size_t l = strlen (p);
  2423.       if (l == n)
  2424.     return 0;
  2425.       l++;
  2426.       p += l;
  2427.       n -= l;
  2428.     }
  2429.   }
  2430. }
  2431.  
  2432. /* Pre-C-Preprocessor to translate ANSI trigraph idiocy in BUF
  2433.    before main CCCP processing.  Name `pcp' is also in honor of the
  2434.    drugs the trigraph designers must have been on.
  2435.  
  2436.    Using an extra pass through the buffer takes a little extra time,
  2437.    but is infinitely less hairy than trying to handle trigraphs inside
  2438.    strings, etc. everywhere, and also makes sure that trigraphs are
  2439.    only translated in the top level of processing. */
  2440.  
  2441. static void
  2442. trigraph_pcp (buf)
  2443.      FILE_BUF *buf;
  2444. {
  2445.   register U_CHAR c, *fptr, *bptr, *sptr, *lptr;
  2446.   int len;
  2447.  
  2448.   fptr = bptr = sptr = buf->buf;
  2449.   lptr = fptr + buf->length;
  2450.   while ((sptr = index0 (sptr, '?', (size_t) (lptr - sptr))) != NULL) {
  2451.     if (*++sptr != '?')
  2452.       continue;
  2453.     switch (*++sptr) {
  2454.       case '=':
  2455.       c = '#';
  2456.       break;
  2457.     case '(':
  2458.       c = '[';
  2459.       break;
  2460.     case '/':
  2461.       c = '\\';
  2462.       break;
  2463.     case ')':
  2464.       c = ']';
  2465.       break;
  2466.     case '\'':
  2467.       c = '^';
  2468.       break;
  2469.     case '<':
  2470.       c = '{';
  2471.       break;
  2472.     case '!':
  2473.       c = '|';
  2474.       break;
  2475.     case '>':
  2476.       c = '}';
  2477.       break;
  2478.     case '-':
  2479.       c  = '~';
  2480.       break;
  2481.     case '?':
  2482.       sptr--;
  2483.       continue;
  2484.     default:
  2485.       continue;
  2486.     }
  2487.     len = sptr - fptr - 2;
  2488.  
  2489.     /* BSD doc says bcopy () works right for overlapping strings.  In ANSI
  2490.        C, this will be memmove (). */
  2491.     if (bptr != fptr && len > 0)
  2492.       bcopy ((char *) fptr, (char *) bptr, len);
  2493.  
  2494.     bptr += len;
  2495.     *bptr++ = c;
  2496.     fptr = ++sptr;
  2497.   }
  2498.   len = buf->length - (fptr - buf->buf);
  2499.   if (bptr != fptr && len > 0)
  2500.     bcopy ((char *) fptr, (char *) bptr, len);
  2501.   buf->length -= fptr - bptr;
  2502.   buf->buf[buf->length] = '\0';
  2503.   if (warn_trigraphs && fptr != bptr)
  2504.     warning ("%d trigraph(s) encountered", (fptr - bptr) / 2);
  2505. }
  2506.  
  2507.  
  2508. /* Replace any CR found in file with a LF; does not change the length
  2509.    of the file */
  2510.  
  2511. static void
  2512. msdos_pcp (buf)
  2513.      FILE_BUF *buf;
  2514. {
  2515.   register U_CHAR *sptr, *lptr;
  2516.  
  2517.   sptr = buf->buf;
  2518.   lptr = sptr + buf->length;
  2519.   while ((sptr = index0 (sptr, '\r', (size_t) (lptr - sptr))) != NULL) {
  2520.     if (sptr[1]=='\n') sptr[1] = ' ';
  2521.     *sptr++ = '\n';
  2522.   }
  2523. }
  2524.  
  2525. /* Move all backslash-newline pairs out of embarrassing places.
  2526.    Exchange all such pairs following BP
  2527.    with any potentially-embarrassing characters that follow them.
  2528.    Potentially-embarrassing characters are / and *
  2529.    (because a backslash-newline inside a comment delimiter
  2530.    would cause it not to be recognized).  */
  2531.  
  2532. static void
  2533. newline_fix (bp)
  2534.      U_CHAR *bp;
  2535. {
  2536.   register U_CHAR *p = bp;
  2537.  
  2538.   /* First count the backslash-newline pairs here.  */
  2539.  
  2540.   while (p[0] == '\\' && p[1] == '\n')
  2541.     p += 2;
  2542.  
  2543.   /* What follows the backslash-newlines is not embarrassing.  */
  2544.  
  2545.   if (*p != '/' && *p != '*')
  2546.     return;
  2547.  
  2548.   /* Copy all potentially embarrassing characters
  2549.      that follow the backslash-newline pairs
  2550.      down to where the pairs originally started.  */
  2551.  
  2552.   while (*p == '*' || *p == '/')
  2553.     *bp++ = *p++;
  2554.  
  2555.   /* Now write the same number of pairs after the embarrassing chars.  */
  2556.   while (bp < p) {
  2557.     *bp++ = '\\';
  2558.     *bp++ = '\n';
  2559.   }
  2560. }
  2561.  
  2562. /* Like newline_fix but for use within a directive-name.
  2563.    Move any backslash-newlines up past any following symbol constituents.  */
  2564.  
  2565. static void
  2566. name_newline_fix (bp)
  2567.      U_CHAR *bp;
  2568. {
  2569.   register U_CHAR *p = bp;
  2570.  
  2571.   /* First count the backslash-newline pairs here.  */
  2572.   while (p[0] == '\\' && p[1] == '\n')
  2573.     p += 2;
  2574.  
  2575.   /* What follows the backslash-newlines is not embarrassing.  */
  2576.  
  2577.   if (!is_idchar[*p])
  2578.     return;
  2579.  
  2580.   /* Copy all potentially embarrassing characters
  2581.      that follow the backslash-newline pairs
  2582.      down to where the pairs originally started.  */
  2583.  
  2584.   while (is_idchar[*p])
  2585.     *bp++ = *p++;
  2586.  
  2587.   /* Now write the same number of pairs after the embarrassing chars.  */
  2588.   while (bp < p) {
  2589.     *bp++ = '\\';
  2590.     *bp++ = '\n';
  2591.   }
  2592. }
  2593.  
  2594. /* Look for lint commands in comments.
  2595.  
  2596.    When we come in here, ibp points into a comment.  Limit is as one expects.
  2597.    scan within the comment -- it should start, after lwsp, with a lint command.
  2598.    If so that command is returned as a (constant) string.
  2599.  
  2600.    Upon return, any arg will be pointed to with argstart and will be
  2601.    arglen long.  Note that we don't parse that arg since it will just
  2602.    be printed out again.
  2603. */
  2604.  
  2605. static char *
  2606. get_lintcmd (ibp, limit, argstart, arglen, cmdlen)
  2607.      register U_CHAR *ibp;
  2608.      register U_CHAR *limit;
  2609.      U_CHAR **argstart;        /* point to command arg */
  2610.      int *arglen, *cmdlen;    /* how long they are */
  2611. {
  2612.   long linsize;
  2613.   register U_CHAR *numptr;    /* temp for arg parsing */
  2614.  
  2615.   *arglen = 0;
  2616.  
  2617.   SKIP_WHITE_SPACE (ibp);
  2618.  
  2619.   if (ibp >= limit) return NULL;
  2620.  
  2621.   linsize = limit - ibp;
  2622.   
  2623.   /* Oh, I wish C had lexical functions... hell, I'll just open-code the set */
  2624.   if ((linsize >= 10) && !bcmp (ibp, "NOTREACHED", 10)) {
  2625.     *cmdlen = 10;
  2626.     return "NOTREACHED";
  2627.   }
  2628.   if ((linsize >= 8) && !bcmp (ibp, "ARGSUSED", 8)) {
  2629.     *cmdlen = 8;
  2630.     return "ARGSUSED";
  2631.   }
  2632.   if ((linsize >= 11) && !bcmp (ibp, "LINTLIBRARY", 11)) {
  2633.     *cmdlen = 11;
  2634.     return "LINTLIBRARY";
  2635.   }
  2636.   if ((linsize >= 7) && !bcmp (ibp, "VARARGS", 7)) {
  2637.     *cmdlen = 7;
  2638.     ibp += 7; linsize -= 7;
  2639.     if ((linsize == 0) || ! isdigit (*ibp)) return "VARARGS";
  2640.  
  2641.     /* OK, read a number */
  2642.     for (numptr = *argstart = ibp; (numptr < limit) && isdigit (*numptr);
  2643.      numptr++);
  2644.     *arglen = numptr - *argstart;
  2645.     return "VARARGS";
  2646.   }
  2647.   return NULL;
  2648. }
  2649.  
  2650. /*
  2651.  * The main loop of the program.
  2652.  *
  2653.  * Read characters from the input stack, transferring them to the
  2654.  * output buffer OP.
  2655.  *
  2656.  * Macros are expanded and push levels on the input stack.
  2657.  * At the end of such a level it is popped off and we keep reading.
  2658.  * At the end of any other kind of level, we return.
  2659.  * #-directives are handled, except within macros.
  2660.  *
  2661.  * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
  2662.  * and insert them when appropriate.  This is set while scanning macro
  2663.  * arguments before substitution.  It is zero when scanning for final output.
  2664.  *   There are three types of Newline markers:
  2665.  *   * Newline -  follows a macro name that was not expanded
  2666.  *     because it appeared inside an expansion of the same macro.
  2667.  *     This marker prevents future expansion of that identifier.
  2668.  *     When the input is rescanned into the final output, these are deleted.
  2669.  *     These are also deleted by ## concatenation.
  2670.  *   * Newline Space (or Newline and any other whitespace character)
  2671.  *     stands for a place that tokens must be separated or whitespace
  2672.  *     is otherwise desirable, but where the ANSI standard specifies there
  2673.  *     is no whitespace.  This marker turns into a Space (or whichever other
  2674.  *     whitespace char appears in the marker) in the final output,
  2675.  *     but it turns into nothing in an argument that is stringified with #.
  2676.  *     Such stringified arguments are the only place where the ANSI standard
  2677.  *     specifies with precision that whitespace may not appear.
  2678.  *
  2679.  * During this function, IP->bufp is kept cached in IBP for speed of access.
  2680.  * Likewise, OP->bufp is kept in OBP.  Before calling a subroutine
  2681.  * IBP, IP and OBP must be copied back to memory.  IP and IBP are
  2682.  * copied back with the RECACHE macro.  OBP must be copied back from OP->bufp
  2683.  * explicitly, and before RECACHE, since RECACHE uses OBP.
  2684.  */
  2685.  
  2686. static void
  2687. rescan (op, output_marks)
  2688.      FILE_BUF *op;
  2689.      int output_marks;
  2690. {
  2691.   /* Character being scanned in main loop.  */
  2692.   register U_CHAR c;
  2693.  
  2694.   /* Length of pending accumulated identifier.  */
  2695.   register int ident_length = 0;
  2696.  
  2697.   /* Hash code of pending accumulated identifier.  */
  2698.   register int hash = 0;
  2699.  
  2700.   /* Current input level (&instack[indepth]).  */
  2701.   FILE_BUF *ip;
  2702.  
  2703.   /* Pointer for scanning input.  */
  2704.   register U_CHAR *ibp;
  2705.  
  2706.   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
  2707.   register U_CHAR *limit;
  2708.  
  2709.   /* Pointer for storing output.  */
  2710.   register U_CHAR *obp;
  2711.  
  2712.   /* REDO_CHAR is nonzero if we are processing an identifier
  2713.      after backing up over the terminating character.
  2714.      Sometimes we process an identifier without backing up over
  2715.      the terminating character, if the terminating character
  2716.      is not special.  Backing up is done so that the terminating character
  2717.      will be dispatched on again once the identifier is dealt with.  */
  2718.   int redo_char = 0;
  2719.  
  2720.   /* 1 if within an identifier inside of which a concatenation
  2721.      marker (Newline -) has been seen.  */
  2722.   int concatenated = 0;
  2723.  
  2724.   /* While scanning a comment or a string constant,
  2725.      this records the line it started on, for error messages.  */
  2726.   int start_line;
  2727.  
  2728.   /* Record position of last `real' newline.  */
  2729.   U_CHAR *beg_of_line;
  2730.  
  2731. /* Pop the innermost input stack level, assuming it is a macro expansion.  */
  2732.  
  2733. #define POPMACRO \
  2734. do { ip->macro->type = T_MACRO;        \
  2735.      if (ip->free_ptr) free (ip->free_ptr);    \
  2736.      --indepth; } while (0)
  2737.  
  2738. /* Reload `rescan's local variables that describe the current
  2739.    level of the input stack.  */
  2740.  
  2741. #define RECACHE  \
  2742. do { ip = &instack[indepth];        \
  2743.      ibp = ip->bufp;            \
  2744.      limit = ip->buf + ip->length;    \
  2745.      op->bufp = obp;            \
  2746.      check_expand (op, limit - ibp);    \
  2747.      beg_of_line = 0;            \
  2748.      obp = op->bufp; } while (0)
  2749.  
  2750.   if (no_output && instack[indepth].fname != 0)
  2751.     skip_if_group (&instack[indepth], 1, NULL);
  2752.  
  2753.   obp = op->bufp;
  2754.   RECACHE;
  2755.  
  2756.   beg_of_line = ibp;
  2757.  
  2758.   /* Our caller must always put a null after the end of
  2759.      the input at each input stack level.  */
  2760.   if (*limit != 0)
  2761.     abort ();
  2762.  
  2763.   while (1) {
  2764.     c = *ibp++;
  2765.     *obp++ = c;
  2766.  
  2767.     switch (c) {
  2768.     case '\\':
  2769.       if (*ibp == '\n' && !ip->macro) {
  2770.     /* At the top level, always merge lines ending with backslash-newline,
  2771.        even in middle of identifier.  But do not merge lines in a macro,
  2772.        since backslash might be followed by a newline-space marker.  */
  2773.     ++ibp;
  2774.     ++ip->lineno;
  2775.     --obp;        /* remove backslash from obuf */
  2776.     break;
  2777.       }
  2778.       /* If ANSI, backslash is just another character outside a string.  */
  2779.       if (!traditional)
  2780.     goto randomchar;
  2781.       /* Otherwise, backslash suppresses specialness of following char,
  2782.      so copy it here to prevent the switch from seeing it.
  2783.      But first get any pending identifier processed.  */
  2784.       if (ident_length > 0)
  2785.     goto specialchar;
  2786.       if (ibp < limit)
  2787.     *obp++ = *ibp++;
  2788.       break;
  2789.  
  2790.     case '%':
  2791.       if (ident_length || ip->macro || traditional)
  2792.     goto randomchar;
  2793.       while (*ibp == '\\' && ibp[1] == '\n') {
  2794.     ibp += 2;
  2795.     ++ip->lineno;
  2796.       }
  2797.       if (*ibp != ':')
  2798.     break;
  2799.       /* Treat this %: digraph as if it were #.  */
  2800.       /* Fall through.  */
  2801.  
  2802.     case '#':
  2803.       if (assertions_flag) {
  2804.     /* Copy #foo (bar lose) without macro expansion.  */
  2805.     obp[-1] = '#';    /* In case it was '%'. */
  2806.     SKIP_WHITE_SPACE (ibp);
  2807.     while (is_idchar[*ibp])
  2808.       *obp++ = *ibp++;
  2809.     SKIP_WHITE_SPACE (ibp);
  2810.     if (*ibp == '(') {
  2811.       ip->bufp = ibp;
  2812.       skip_paren_group (ip);
  2813.       bcopy ((char *) ibp, (char *) obp, ip->bufp - ibp);
  2814.       obp += ip->bufp - ibp;
  2815.       ibp = ip->bufp;
  2816.     }
  2817.       }
  2818.  
  2819.       /* If this is expanding a macro definition, don't recognize
  2820.      preprocessing directives.  */
  2821.       if (ip->macro != 0)
  2822.     goto randomchar;
  2823.       /* If this is expand_into_temp_buffer,
  2824.      don't recognize them either.  Warn about them
  2825.      only after an actual newline at this level,
  2826.      not at the beginning of the input level.  */
  2827.       if (! ip->fname) {
  2828.     if (ip->buf != beg_of_line)
  2829.       warning ("preprocessing directive not recognized within macro arg");
  2830.     goto randomchar;
  2831.       }
  2832.       if (ident_length)
  2833.     goto specialchar;
  2834.  
  2835.       
  2836.       /* # keyword: a # must be first nonblank char on the line */
  2837.       if (beg_of_line == 0)
  2838.     goto randomchar;
  2839.       {
  2840.     U_CHAR *bp;
  2841.  
  2842.     /* Scan from start of line, skipping whitespace, comments
  2843.        and backslash-newlines, and see if we reach this #.
  2844.        If not, this # is not special.  */
  2845.     bp = beg_of_line;
  2846.     /* If -traditional, require # to be at beginning of line.  */
  2847.     if (!traditional) {
  2848.       while (1) {
  2849.         if (is_hor_space[*bp])
  2850.           bp++;
  2851.         else if (*bp == '\\' && bp[1] == '\n')
  2852.           bp += 2;
  2853.         else if (*bp == '/' && bp[1] == '*') {
  2854.           bp += 2;
  2855.           while (!(*bp == '*' && bp[1] == '/'))
  2856.         bp++;
  2857.           bp += 2;
  2858.         }
  2859.         /* There is no point in trying to deal with C++ // comments here,
  2860.            because if there is one, then this # must be part of the
  2861.            comment and we would never reach here.  */
  2862.         else break;
  2863.       }
  2864.       if (c == '%') {
  2865.         if (bp[0] != '%')
  2866.           break;
  2867.         while (bp[1] == '\\' && bp[2] == '\n')
  2868.           bp += 2;
  2869.         if (bp + 1 != ibp)
  2870.           break;
  2871.         /* %: appears at start of line; skip past the ':' too.  */
  2872.         bp++;
  2873.         ibp++;
  2874.       }
  2875.     }
  2876.     if (bp + 1 != ibp)
  2877.       goto randomchar;
  2878.       }
  2879.  
  2880.       /* This # can start a directive.  */
  2881.  
  2882.       --obp;        /* Don't copy the '#' */
  2883.  
  2884.       ip->bufp = ibp;
  2885.       op->bufp = obp;
  2886.       if (! handle_directive (ip, op)) {
  2887. #ifdef USE_C_ALLOCA
  2888.     alloca (0);
  2889. #endif
  2890.     /* Not a known directive: treat it as ordinary text.
  2891.        IP, OP, IBP, etc. have not been changed.  */
  2892.     if (no_output && instack[indepth].fname) {
  2893.       /* If not generating expanded output,
  2894.          what we do with ordinary text is skip it.
  2895.          Discard everything until next # directive.  */
  2896.       skip_if_group (&instack[indepth], 1, 0);
  2897.       RECACHE;
  2898.       beg_of_line = ibp;
  2899.       break;
  2900.     }
  2901.     *obp++ = '#';    /* Copy # (even if it was originally %:).  */
  2902.     /* Don't expand an identifier that could be a macro directive.
  2903.        (Section 3.8.3 of the ANSI C standard)            */
  2904.     SKIP_WHITE_SPACE (ibp);
  2905.     if (is_idstart[*ibp])
  2906.       {
  2907.         *obp++ = *ibp++;
  2908.         while (is_idchar[*ibp])
  2909.           *obp++ = *ibp++;
  2910.       }
  2911.     goto randomchar;
  2912.       }
  2913. #ifdef USE_C_ALLOCA
  2914.       alloca (0);
  2915. #endif
  2916.       /* A # directive has been successfully processed.  */
  2917.       /* If not generating expanded output, ignore everything until
  2918.      next # directive.  */
  2919.       if (no_output && instack[indepth].fname)
  2920.     skip_if_group (&instack[indepth], 1, 0);
  2921.       obp = op->bufp;
  2922.       RECACHE;
  2923.       beg_of_line = ibp;
  2924.       break;
  2925.  
  2926.     case '\"':            /* skip quoted string */
  2927.     case '\'':
  2928.       /* A single quoted string is treated like a double -- some
  2929.      programs (e.g., troff) are perverse this way */
  2930.  
  2931.       if (ident_length)
  2932.     goto specialchar;
  2933.  
  2934.       start_line = ip->lineno;
  2935.  
  2936.       /* Skip ahead to a matching quote.  */
  2937.  
  2938.       while (1) {
  2939.     if (ibp >= limit) {
  2940.       if (ip->macro != 0) {
  2941.         /* try harder: this string crosses a macro expansion boundary.
  2942.            This can happen naturally if -traditional.
  2943.            Otherwise, only -D can make a macro with an unmatched quote.  */
  2944.         POPMACRO;
  2945.         RECACHE;
  2946.         continue;
  2947.       }
  2948.       if (!traditional) {
  2949.         error_with_line (line_for_error (start_line),
  2950.                  "unterminated string or character constant");
  2951.         error_with_line (multiline_string_line,
  2952.                  "possible real start of unterminated constant");
  2953.         multiline_string_line = 0;
  2954.       }
  2955.       break;
  2956.     }
  2957.     *obp++ = *ibp;
  2958.     switch (*ibp++) {
  2959.     case '\n':
  2960.       ++ip->lineno;
  2961.       ++op->lineno;
  2962.       /* Traditionally, end of line ends a string constant with no error.
  2963.          So exit the loop and record the new line.  */
  2964.       if (traditional) {
  2965.         beg_of_line = ibp;
  2966.         goto while2end;
  2967.       }
  2968.       if (c == '\'') {
  2969.         error_with_line (line_for_error (start_line),
  2970.                  "unterminated character constant");
  2971.         goto while2end;
  2972.       }
  2973.       if (pedantic && multiline_string_line == 0) {
  2974.         pedwarn_with_line (line_for_error (start_line),
  2975.                    "string constant runs past end of line");
  2976.       }
  2977.       if (multiline_string_line == 0)
  2978.         multiline_string_line = ip->lineno - 1;
  2979.       break;
  2980.  
  2981.     case '\\':
  2982.       if (ibp >= limit)
  2983.         break;
  2984.       if (*ibp == '\n') {
  2985.         /* Backslash newline is replaced by nothing at all,
  2986.            but keep the line counts correct.  */
  2987.         --obp;
  2988.         ++ibp;
  2989.         ++ip->lineno;
  2990.       } else {
  2991.         /* ANSI stupidly requires that in \\ the second \
  2992.            is *not* prevented from combining with a newline.  */
  2993.         while (*ibp == '\\' && ibp[1] == '\n') {
  2994.           ibp += 2;
  2995.           ++ip->lineno;
  2996.         }
  2997.         *obp++ = *ibp++;
  2998.       }
  2999.       break;
  3000.  
  3001.     case '\"':
  3002.     case '\'':
  3003.       if (ibp[-1] == c)
  3004.         goto while2end;
  3005.       break;
  3006.     }
  3007.       }
  3008.     while2end:
  3009.       break;
  3010.  
  3011.     case '/':
  3012.       if (*ibp == '\\' && ibp[1] == '\n')
  3013.     newline_fix (ibp);
  3014.  
  3015.       if (*ibp != '*'
  3016.       && !(cplusplus_comments && *ibp == '/'))
  3017.     goto randomchar;
  3018.       if (ip->macro != 0)
  3019.     goto randomchar;
  3020.       if (ident_length)
  3021.     goto specialchar;
  3022.  
  3023.       if (*ibp == '/') {
  3024.     /* C++ style comment... */
  3025.     start_line = ip->lineno;
  3026.  
  3027.     --ibp;            /* Back over the slash */
  3028.     --obp;
  3029.  
  3030.     /* Comments are equivalent to spaces. */
  3031.     if (! put_out_comments)
  3032.       *obp++ = ' ';
  3033.     else {
  3034.       /* must fake up a comment here */
  3035.       *obp++ = '/';
  3036.       *obp++ = '/';
  3037.     }
  3038.     {
  3039.       U_CHAR *before_bp = ibp+2;
  3040.  
  3041.       while (ibp < limit) {
  3042.         if (ibp[-1] != '\\' && *ibp == '\n') {
  3043.           if (put_out_comments) {
  3044.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3045.         obp += ibp - before_bp;
  3046.           }
  3047.           break;
  3048.         } else {
  3049.           if (*ibp == '\n') {
  3050.         ++ip->lineno;
  3051.         /* Copy the newline into the output buffer, in order to
  3052.            avoid the pain of a #line every time a multiline comment
  3053.            is seen.  */
  3054.         if (!put_out_comments)
  3055.           *obp++ = '\n';
  3056.         ++op->lineno;
  3057.           }
  3058.           ibp++;
  3059.         }
  3060.       }
  3061.       break;
  3062.     }
  3063.       }
  3064.  
  3065.       /* Ordinary C comment.  Skip it, optionally copying it to output.  */
  3066.  
  3067.       start_line = ip->lineno;
  3068.  
  3069.       ++ibp;            /* Skip the star. */
  3070.  
  3071.       /* If this cpp is for lint, we peek inside the comments: */
  3072.       if (for_lint) {
  3073.     U_CHAR *argbp;
  3074.     int cmdlen, arglen;
  3075.     char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
  3076.  
  3077.     if (lintcmd != NULL) {
  3078.       op->bufp = obp;
  3079.       check_expand (op, cmdlen + arglen + 14);
  3080.       obp = op->bufp;
  3081.       /* I believe it is always safe to emit this newline: */
  3082.       obp[-1] = '\n';
  3083.       bcopy ("#pragma lint ", (char *) obp, 13);
  3084.       obp += 13;
  3085.       bcopy (lintcmd, (char *) obp, cmdlen);
  3086.       obp += cmdlen;
  3087.  
  3088.       if (arglen != 0) {
  3089.         *(obp++) = ' ';
  3090.         bcopy (argbp, (char *) obp, arglen);
  3091.         obp += arglen;
  3092.       }
  3093.  
  3094.       /* OK, now bring us back to the state we were in before we entered
  3095.          this branch.  We need #line because the #pragma's newline always
  3096.          messes up the line count.  */
  3097.       op->bufp = obp;
  3098.       output_line_directive (ip, op, 0, same_file);
  3099.       check_expand (op, limit - ibp + 2);
  3100.       obp = op->bufp;
  3101.       *(obp++) = '/';
  3102.     }
  3103.       }
  3104.  
  3105.       /* Comments are equivalent to spaces.
  3106.      Note that we already output the slash; we might not want it.
  3107.      For -traditional, a comment is equivalent to nothing.  */
  3108.       if (! put_out_comments) {
  3109.     if (traditional)
  3110.       obp--;
  3111.     else
  3112.       obp[-1] = ' ';
  3113.       }
  3114.       else
  3115.     *obp++ = '*';
  3116.  
  3117.       {
  3118.     U_CHAR *before_bp = ibp;
  3119.  
  3120.     while (ibp < limit) {
  3121.       switch (*ibp++) {
  3122.       case '/':
  3123.         if (warn_comments && *ibp == '*')
  3124.           warning ("`/*' within comment");
  3125.         break;
  3126.       case '*':
  3127.         if (*ibp == '\\' && ibp[1] == '\n')
  3128.           newline_fix (ibp);
  3129.         if (ibp >= limit || *ibp == '/')
  3130.           goto comment_end;
  3131.         break;
  3132.       case '\n':
  3133.         ++ip->lineno;
  3134.         /* Copy the newline into the output buffer, in order to
  3135.            avoid the pain of a #line every time a multiline comment
  3136.            is seen.  */
  3137.         if (!put_out_comments)
  3138.           *obp++ = '\n';
  3139.         ++op->lineno;
  3140.       }
  3141.     }
  3142.       comment_end:
  3143.  
  3144.     if (ibp >= limit)
  3145.       error_with_line (line_for_error (start_line),
  3146.                "unterminated comment");
  3147.     else {
  3148.       ibp++;
  3149.       if (put_out_comments) {
  3150.         bcopy ((char *) before_bp, (char *) obp, ibp - before_bp);
  3151.         obp += ibp - before_bp;
  3152.       }
  3153.     }
  3154.       }
  3155.       break;
  3156.  
  3157.     case '$':
  3158.       if (!dollars_in_ident)
  3159.     goto randomchar;
  3160.       goto letter;
  3161.  
  3162.     case '0': case '1': case '2': case '3': case '4':
  3163.     case '5': case '6': case '7': case '8': case '9':
  3164.       /* If digit is not part of identifier, it starts a number,
  3165.      which means that following letters are not an identifier.
  3166.      "0x5" does not refer to an identifier "x5".
  3167.      So copy all alphanumerics that follow without accumulating
  3168.      as an identifier.  Periods also, for sake of "3.e7".  */
  3169.  
  3170.       if (ident_length == 0) {
  3171.     for (;;) {
  3172.       while (ibp[0] == '\\' && ibp[1] == '\n') {
  3173.         ++ip->lineno;
  3174.         ibp += 2;
  3175.       }
  3176.       c = *ibp++;
  3177.       if (!is_idchar[c] && c != '.') {
  3178.         --ibp;
  3179.         break;
  3180.       }
  3181.       *obp++ = c;
  3182.       /* A sign can be part of a preprocessing number
  3183.          if it follows an e.  */
  3184.       if (c == 'e' || c == 'E') {
  3185.         while (ibp[0] == '\\' && ibp[1] == '\n') {
  3186.           ++ip->lineno;
  3187.           ibp += 2;
  3188.         }
  3189.         if (*ibp == '+' || *ibp == '-') {
  3190.           *obp++ = *ibp++;
  3191.           /* But traditional C does not let the token go past the sign.  */
  3192.           if (traditional)
  3193.         break;
  3194.         }
  3195.       }
  3196.     }
  3197.     break;
  3198.       }
  3199.       /* fall through */
  3200.  
  3201.     case '_':
  3202.     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  3203.     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
  3204.     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
  3205.     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
  3206.     case 'y': case 'z':
  3207.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
  3208.     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
  3209.     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
  3210.     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
  3211.     case 'Y': case 'Z':
  3212.     letter:
  3213.       ident_length++;
  3214.       /* Compute step of hash function, to avoid a proc call on every token */
  3215.       hash = HASHSTEP (hash, c);
  3216.       break;
  3217.  
  3218.     case '\n':
  3219.       if (ip->fname == 0 && *ibp == '-') {
  3220.     /* Newline - inhibits expansion of preceding token.
  3221.        If expanding a macro arg, we keep the newline -.
  3222.        In final output, it is deleted.
  3223.        We recognize Newline - in macro bodies and macro args.  */
  3224.     if (! concatenated) {
  3225.       ident_length = 0;
  3226.       hash = 0;
  3227.     }
  3228.     ibp++;
  3229.     if (!output_marks) {
  3230.       obp--;
  3231.     } else {
  3232.       /* If expanding a macro arg, keep the newline -.  */
  3233.       *obp++ = '-';
  3234.     }
  3235.     break;
  3236.       }
  3237.  
  3238.       /* If reprocessing a macro expansion, newline is a special marker.  */
  3239.       else if (ip->macro != 0) {
  3240.     /* Newline White is a "funny space" to separate tokens that are
  3241.        supposed to be separate but without space between.
  3242.        Here White means any whitespace character.
  3243.        Newline - marks a recursive macro use that is not
  3244.        supposed to be expandable.  */
  3245.  
  3246.     if (is_space[*ibp]) {
  3247.       /* Newline Space does not prevent expansion of preceding token
  3248.          so expand the preceding token and then come back.  */
  3249.       if (ident_length > 0)
  3250.         goto specialchar;
  3251.  
  3252.       /* If generating final output, newline space makes a space.  */
  3253.       if (!output_marks) {
  3254.         obp[-1] = *ibp++;
  3255.         /* And Newline Newline makes a newline, so count it.  */
  3256.         if (obp[-1] == '\n')
  3257.           op->lineno++;
  3258.       } else {
  3259.         /* If expanding a macro arg, keep the newline space.
  3260.            If the arg gets stringified, newline space makes nothing.  */
  3261.         *obp++ = *ibp++;
  3262.       }
  3263.     } else abort ();    /* Newline followed by something random?  */
  3264.     break;
  3265.       }
  3266.  
  3267.       /* If there is a pending identifier, handle it and come back here.  */
  3268.       if (ident_length > 0)
  3269.     goto specialchar;
  3270.  
  3271.       beg_of_line = ibp;
  3272.  
  3273.       /* Update the line counts and output a #line if necessary.  */
  3274.       ++ip->lineno;
  3275.       ++op->lineno;
  3276.       if (ip->lineno != op->lineno) {
  3277.     op->bufp = obp;
  3278.     output_line_directive (ip, op, 1, same_file);
  3279.     check_expand (op, limit - ibp);
  3280.     obp = op->bufp;
  3281.       }
  3282.       break;
  3283.  
  3284.       /* Come here either after (1) a null character that is part of the input
  3285.      or (2) at the end of the input, because there is a null there.  */
  3286.     case 0:
  3287.       if (ibp <= limit)
  3288.     /* Our input really contains a null character.  */
  3289.     goto randomchar;
  3290.  
  3291.       /* At end of a macro-expansion level, pop it and read next level.  */
  3292.       if (ip->macro != 0) {
  3293.     obp--;
  3294.     ibp--;
  3295.     /* If traditional, and we have an identifier that ends here,
  3296.        process it now, so we get the right error for recursion.  */
  3297.     if (traditional && ident_length
  3298.         && ! is_idchar[*instack[indepth - 1].bufp]) {
  3299.       redo_char = 1;
  3300.       goto randomchar;
  3301.     }
  3302.     POPMACRO;
  3303.     RECACHE;
  3304.     break;
  3305.       }
  3306.  
  3307.       /* If we don't have a pending identifier,
  3308.      return at end of input.  */
  3309.       if (ident_length == 0) {
  3310.     obp--;
  3311.     ibp--;
  3312.     op->bufp = obp;
  3313.     ip->bufp = ibp;
  3314.     goto ending;
  3315.       }
  3316.  
  3317.       /* If we do have a pending identifier, just consider this null
  3318.      a special character and arrange to dispatch on it again.
  3319.      The second time, IDENT_LENGTH will be zero so we will return.  */
  3320.  
  3321.       /* Fall through */
  3322.  
  3323. specialchar:
  3324.  
  3325.       /* Handle the case of a character such as /, ', " or null
  3326.      seen following an identifier.  Back over it so that
  3327.      after the identifier is processed the special char
  3328.      will be dispatched on again.  */
  3329.  
  3330.       ibp--;
  3331.       obp--;
  3332.       redo_char = 1;
  3333.  
  3334.     default:
  3335.  
  3336. randomchar:
  3337.  
  3338.       if (ident_length > 0) {
  3339.     register HASHNODE *hp;
  3340.  
  3341.     /* We have just seen an identifier end.  If it's a macro, expand it.
  3342.  
  3343.        IDENT_LENGTH is the length of the identifier
  3344.        and HASH is its hash code.
  3345.  
  3346.        The identifier has already been copied to the output,
  3347.        so if it is a macro we must remove it.
  3348.  
  3349.        If REDO_CHAR is 0, the char that terminated the identifier
  3350.        has been skipped in the output and the input.
  3351.        OBP-IDENT_LENGTH-1 points to the identifier.
  3352.        If the identifier is a macro, we must back over the terminator.
  3353.  
  3354.        If REDO_CHAR is 1, the terminating char has already been
  3355.        backed over.  OBP-IDENT_LENGTH points to the identifier.  */
  3356.  
  3357.     if (!pcp_outfile || pcp_inside_if) {
  3358.       for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
  3359.            hp = hp->next) {
  3360.         
  3361.         if (hp->length == ident_length) {
  3362.           int obufp_before_macroname;
  3363.           int op_lineno_before_macroname;
  3364.           register int i = ident_length;
  3365.           register U_CHAR *p = hp->name;
  3366.           register U_CHAR *q = obp - i;
  3367.           int disabled;
  3368.           
  3369.           if (! redo_char)
  3370.         q--;
  3371.           
  3372.           do {        /* All this to avoid a strncmp () */
  3373.         if (*p++ != *q++)
  3374.           goto hashcollision;
  3375.           } while (--i);
  3376.           
  3377.           /* We found a use of a macro name.
  3378.          see if the context shows it is a macro call.  */
  3379.           
  3380.           /* Back up over terminating character if not already done.  */
  3381.           if (! redo_char) {
  3382.         ibp--;
  3383.         obp--;
  3384.           }
  3385.           
  3386.           /* Save this as a displacement from the beginning of the output
  3387.          buffer.  We can not save this as a position in the output
  3388.          buffer, because it may get realloc'ed by RECACHE.  */
  3389.           obufp_before_macroname = (obp - op->buf) - ident_length;
  3390.           op_lineno_before_macroname = op->lineno;
  3391.           
  3392.           if (hp->type == T_PCSTRING) {
  3393.         pcstring_used (hp); /* Mark the definition of this key
  3394.                        as needed, ensuring that it
  3395.                        will be output.  */
  3396.         break;        /* Exit loop, since the key cannot have a
  3397.                    definition any longer.  */
  3398.           }
  3399.  
  3400.           /* Record whether the macro is disabled.  */
  3401.           disabled = hp->type == T_DISABLED;
  3402.           
  3403.           /* This looks like a macro ref, but if the macro was disabled,
  3404.          just copy its name and put in a marker if requested.  */
  3405.           
  3406.           if (disabled) {
  3407. #if 0
  3408.         /* This error check caught useful cases such as
  3409.            #define foo(x,y) bar (x (y,0), y)
  3410.            foo (foo, baz)  */
  3411.         if (traditional)
  3412.           error ("recursive use of macro `%s'", hp->name);
  3413. #endif
  3414.         
  3415.         if (output_marks) {
  3416.           check_expand (op, limit - ibp + 2);
  3417.           *obp++ = '\n';
  3418.           *obp++ = '-';
  3419.         }
  3420.         break;
  3421.           }
  3422.           
  3423.           /* If macro wants an arglist, verify that a '(' follows.
  3424.          first skip all whitespace, copying it to the output
  3425.          after the macro name.  Then, if there is no '(',
  3426.          decide this is not a macro call and leave things that way.  */
  3427.           if ((hp->type == T_MACRO || hp->type == T_DISABLED)
  3428.           && hp->value.defn->nargs >= 0)
  3429.         {
  3430.           U_CHAR *old_ibp = ibp;
  3431.           U_CHAR *old_obp = obp;
  3432.           int old_iln = ip->lineno;
  3433.           int old_oln = op->lineno;
  3434.           
  3435.           while (1) {
  3436.             /* Scan forward over whitespace, copying it to the output.  */
  3437.             if (ibp == limit && ip->macro != 0) {
  3438.               POPMACRO;
  3439.               RECACHE;
  3440.               old_ibp = ibp;
  3441.               old_obp = obp;
  3442.               old_iln = ip->lineno;
  3443.               old_oln = op->lineno;
  3444.             }
  3445.             /* A comment: copy it unchanged or discard it.  */
  3446.             else if (*ibp == '/' && ibp[1] == '*') {
  3447.               if (put_out_comments) {
  3448.             *obp++ = '/';
  3449.             *obp++ = '*';
  3450.               } else if (! traditional) {
  3451.             *obp++ = ' ';
  3452.               }
  3453.               ibp += 2;
  3454.               while (ibp + 1 != limit
  3455.                  && !(ibp[0] == '*' && ibp[1] == '/')) {
  3456.             /* We need not worry about newline-marks,
  3457.                since they are never found in comments.  */
  3458.             if (*ibp == '\n') {
  3459.               /* Newline in a file.  Count it.  */
  3460.               ++ip->lineno;
  3461.               ++op->lineno;
  3462.             }
  3463.             if (put_out_comments)
  3464.               *obp++ = *ibp++;
  3465.             else
  3466.               ibp++;
  3467.               }
  3468.               ibp += 2;
  3469.               if (put_out_comments) {
  3470.             *obp++ = '*';
  3471.             *obp++ = '/';
  3472.               }
  3473.             }
  3474.             else if (is_space[*ibp]) {
  3475.               *obp++ = *ibp++;
  3476.               if (ibp[-1] == '\n') {
  3477.             if (ip->macro == 0) {
  3478.               /* Newline in a file.  Count it.  */
  3479.               ++ip->lineno;
  3480.               ++op->lineno;
  3481.             } else if (!output_marks) {
  3482.               /* A newline mark, and we don't want marks
  3483.                  in the output.  If it is newline-hyphen,
  3484.                  discard it entirely.  Otherwise, it is
  3485.                  newline-whitechar, so keep the whitechar.  */
  3486.               obp--;
  3487.               if (*ibp == '-')
  3488.                 ibp++;
  3489.               else {
  3490.                 if (*ibp == '\n')
  3491.                   ++op->lineno;
  3492.                 *obp++ = *ibp++;
  3493.               }
  3494.             } else {
  3495.               /* A newline mark; copy both chars to the output.  */
  3496.               *obp++ = *ibp++;
  3497.             }
  3498.               }
  3499.             }
  3500.             else break;
  3501.           }
  3502.           if (*ibp != '(') {
  3503.             /* It isn't a macro call.
  3504.                Put back the space that we just skipped.  */
  3505.             ibp = old_ibp;
  3506.             obp = old_obp;
  3507.             ip->lineno = old_iln;
  3508.             op->lineno = old_oln;
  3509.             /* Exit the for loop.  */
  3510.             break;
  3511.           }
  3512.         }
  3513.           
  3514.           /* This is now known to be a macro call.
  3515.          Discard the macro name from the output,
  3516.          along with any following whitespace just copied,
  3517.          but preserve newlines if not outputting marks since this
  3518.          is more likely to do the right thing with line numbers.  */
  3519.           obp = op->buf + obufp_before_macroname;
  3520.           if (output_marks)
  3521.         op->lineno = op_lineno_before_macroname;
  3522.           else {
  3523.         int newlines = op->lineno - op_lineno_before_macroname;
  3524.         while (0 < newlines--)
  3525.           *obp++ = '\n';
  3526.           }
  3527.  
  3528.           /* Prevent accidental token-pasting with a character
  3529.          before the macro call.  */
  3530.           if (!traditional && obp != op->buf) {
  3531.         switch (obp[-1]) {
  3532.         case '!':  case '%':  case '&':  case '*':
  3533.         case '+':  case '-':  case '/':  case ':':
  3534.         case '<':  case '=':  case '>':  case '^':
  3535.         case '|':
  3536.           /* If we are expanding a macro arg, make a newline marker
  3537.              to separate the tokens.  If we are making real output,
  3538.              a plain space will do.  */
  3539.           if (output_marks)
  3540.             *obp++ = '\n';
  3541.           *obp++ = ' ';
  3542.         }
  3543.           }
  3544.  
  3545.           /* Expand the macro, reading arguments as needed,
  3546.          and push the expansion on the input stack.  */
  3547.           ip->bufp = ibp;
  3548.           op->bufp = obp;
  3549.           macroexpand (hp, op);
  3550.           
  3551.           /* Reexamine input stack, since macroexpand has pushed
  3552.          a new level on it.  */
  3553.           obp = op->bufp;
  3554.           RECACHE;
  3555.           break;
  3556.         }
  3557. hashcollision:
  3558.         ;
  3559.       }            /* End hash-table-search loop */
  3560.     }
  3561.     ident_length = hash = 0; /* Stop collecting identifier */
  3562.     redo_char = 0;
  3563.     concatenated = 0;
  3564.       }                /* End if (ident_length > 0) */
  3565.     }                /* End switch */
  3566.   }                /* End per-char loop */
  3567.  
  3568.   /* Come here to return -- but first give an error message
  3569.      if there was an unterminated successful conditional.  */
  3570.  ending:
  3571.   if (if_stack != ip->if_stack)
  3572.     {
  3573.       char *str;
  3574.  
  3575.       switch (if_stack->type)
  3576.     {
  3577.     case T_IF:
  3578.       str = "if";
  3579.       break;
  3580.     case T_IFDEF:
  3581.       str = "ifdef";
  3582.       break;
  3583.     case T_IFNDEF:
  3584.       str = "ifndef";
  3585.       break;
  3586.     case T_ELSE:
  3587.       str = "else";
  3588.       break;
  3589.     case T_ELIF:
  3590.       str = "elif";
  3591.       break;
  3592.     default:
  3593.       abort ();
  3594.     }
  3595.  
  3596.       error_with_line (line_for_error (if_stack->lineno),
  3597.                "unterminated `#%s' conditional", str);
  3598.   }
  3599.   if_stack = ip->if_stack;
  3600. }
  3601.  
  3602. /*
  3603.  * Rescan a string into a temporary buffer and return the result
  3604.  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
  3605.  *
  3606.  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
  3607.  * and insert such markers when appropriate.  See `rescan' for details.
  3608.  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
  3609.  * before substitution; it is 0 for other uses.
  3610.  */
  3611. static FILE_BUF
  3612. expand_to_temp_buffer (buf, limit, output_marks, assertions)
  3613.      U_CHAR *buf, *limit;
  3614.      int output_marks, assertions;
  3615. {
  3616.   register FILE_BUF *ip;
  3617.   FILE_BUF obuf;
  3618.   int length = limit - buf;
  3619.   U_CHAR *buf1;
  3620.   int odepth = indepth;
  3621.   int save_assertions_flag = assertions_flag;
  3622.  
  3623.   assertions_flag = assertions;
  3624.  
  3625.   if (length < 0)
  3626.     abort ();
  3627.  
  3628.   /* Set up the input on the input stack.  */
  3629.  
  3630.   buf1 = (U_CHAR *) alloca (length + 1);
  3631.   {
  3632.     register U_CHAR *p1 = buf;
  3633.     register U_CHAR *p2 = buf1;
  3634.  
  3635.     while (p1 != limit)
  3636.       *p2++ = *p1++;
  3637.   }
  3638.   buf1[length] = 0;
  3639.  
  3640.   /* Set up to receive the output.  */
  3641.  
  3642.   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
  3643.   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
  3644.   obuf.fname = 0;
  3645.   obuf.macro = 0;
  3646.   obuf.free_ptr = 0;
  3647.  
  3648.   CHECK_DEPTH ({return obuf;});
  3649.  
  3650.   ++indepth;
  3651.  
  3652.   ip = &instack[indepth];
  3653.   ip->fname = 0;
  3654.   ip->nominal_fname = 0;
  3655.   ip->system_header_p = 0;
  3656.   ip->macro = 0;
  3657.   ip->free_ptr = 0;
  3658.   ip->length = length;
  3659.   ip->buf = ip->bufp = buf1;
  3660.   ip->if_stack = if_stack;
  3661.  
  3662.   ip->lineno = obuf.lineno = 1;
  3663.  
  3664.   /* Scan the input, create the output.  */
  3665.   rescan (&obuf, output_marks);
  3666.  
  3667.   /* Pop input stack to original state.  */
  3668.   --indepth;
  3669.  
  3670.   if (indepth != odepth)
  3671.     abort ();
  3672.  
  3673.   /* Record the output.  */
  3674.   obuf.length = obuf.bufp - obuf.buf;
  3675.  
  3676.   assertions_flag = save_assertions_flag;
  3677.   return obuf;
  3678. }
  3679.  
  3680. /*
  3681.  * Process a # directive.  Expects IP->bufp to point after the '#', as in
  3682.  * `#define foo bar'.  Passes to the directive handler
  3683.  * (do_define, do_include, etc.): the addresses of the 1st and
  3684.  * last chars of the directive (starting immediately after the #
  3685.  * keyword), plus op and the keyword table pointer.  If the directive
  3686.  * contains comments it is copied into a temporary buffer sans comments
  3687.  * and the temporary buffer is passed to the directive handler instead.
  3688.  * Likewise for backslash-newlines.
  3689.  *
  3690.  * Returns nonzero if this was a known # directive.
  3691.  * Otherwise, returns zero, without advancing the input pointer.
  3692.  */
  3693.  
  3694. static int
  3695. handle_directive (ip, op)
  3696.      FILE_BUF *ip, *op;
  3697. {
  3698.   register U_CHAR *bp, *cp;
  3699.   register struct directive *kt;
  3700.   register int ident_length;
  3701.   U_CHAR *resume_p;
  3702.  
  3703.   /* Nonzero means we must copy the entire directive
  3704.      to get rid of comments or backslash-newlines.  */
  3705.   int copy_directive = 0;
  3706.  
  3707.   U_CHAR *ident, *after_ident;
  3708.  
  3709.   bp = ip->bufp;
  3710.  
  3711.   /* Record where the directive started.  do_xifdef needs this.  */
  3712.   directive_start = bp - 1;
  3713.  
  3714.   /* Skip whitespace and \-newline.  */
  3715.   while (1) {
  3716.     if (is_hor_space[*bp]) {
  3717.       if (*bp != ' ' && *bp != '\t' && pedantic)
  3718.     pedwarn ("%s in preprocessing directive", char_name[*bp]);
  3719.       bp++;
  3720.     } else if (*bp == '/' && (bp[1] == '*'
  3721.                   || (cplusplus_comments && bp[1] == '/'))) {
  3722.       ip->bufp = bp + 2;
  3723.       skip_to_end_of_comment (ip, &ip->lineno, 0);
  3724.       bp = ip->bufp;
  3725.     } else if (*bp == '\\' && bp[1] == '\n') {
  3726.       bp += 2; ip->lineno++;
  3727.     } else break;
  3728.   }
  3729.  
  3730.   /* Now find end of directive name.
  3731.      If we encounter a backslash-newline, exchange it with any following
  3732.      symbol-constituents so that we end up with a contiguous name.  */
  3733.  
  3734.   cp = bp;
  3735.   while (1) {
  3736.     if (is_idchar[*cp])
  3737.       cp++;
  3738.     else {
  3739.       if (*cp == '\\' && cp[1] == '\n')
  3740.     name_newline_fix (cp);
  3741.       if (is_idchar[*cp])
  3742.     cp++;
  3743.       else break;
  3744.     }
  3745.   }
  3746.   ident_length = cp - bp;
  3747.   ident = bp;
  3748.   after_ident = cp;
  3749.  
  3750.   /* A line of just `#' becomes blank.  */
  3751.  
  3752.   if (ident_length == 0 && *after_ident == '\n') {
  3753.     ip->bufp = after_ident;
  3754.     return 1;
  3755.   }
  3756.  
  3757.   if (ident_length == 0 || !is_idstart[*ident]) {
  3758.     U_CHAR *p = ident;
  3759.     while (is_idchar[*p]) {
  3760.       if (*p < '0' || *p > '9')
  3761.     break;
  3762.       p++;
  3763.     }
  3764.     /* Handle # followed by a line number.  */
  3765.     if (p != ident && !is_idchar[*p]) {
  3766.       static struct directive line_directive_table[] = {
  3767.     {  4, do_line, "line", T_LINE},
  3768.       };
  3769.       if (pedantic)
  3770.     pedwarn ("`#' followed by integer");
  3771.       after_ident = ident;
  3772.       kt = line_directive_table;
  3773.       goto old_linenum;
  3774.     }
  3775.  
  3776.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  3777.     if (p == ident) {
  3778.       while (*p == '#' || is_hor_space[*p]) p++;
  3779.       if (*p == '\n') {
  3780.     if (pedantic && !lang_asm)
  3781.       warning ("invalid preprocessing directive");
  3782.     return 0;
  3783.       }
  3784.     }
  3785.  
  3786.     if (!lang_asm)
  3787.       error ("invalid preprocessing directive name");
  3788.  
  3789.     return 0;
  3790.   }
  3791.  
  3792.   /*
  3793.    * Decode the keyword and call the appropriate expansion
  3794.    * routine, after moving the input pointer up to the next line.
  3795.    */
  3796.   for (kt = directive_table; kt->length > 0; kt++) {
  3797.     if (kt->length == ident_length && !bcmp (kt->name, ident, ident_length)) {
  3798.       register U_CHAR *buf;
  3799.       register U_CHAR *limit;
  3800.       int unterminated;
  3801.       int junk;
  3802.       int *already_output;
  3803.  
  3804.       /* Nonzero means do not delete comments within the directive.
  3805.      #define needs this when -traditional.  */
  3806.       int keep_comments;
  3807.  
  3808.     old_linenum:
  3809.  
  3810.       limit = ip->buf + ip->length;
  3811.       unterminated = 0;
  3812.       already_output = 0;
  3813.       keep_comments = traditional && kt->traditional_comments;
  3814.       /* #import is defined only in Objective C, or when on the NeXT.  */
  3815.       if (kt->type == T_IMPORT
  3816.       && !(objc || lookup ((U_CHAR *) "__NeXT__", -1, -1)))
  3817.     break;
  3818.  
  3819.       /* Find the end of this directive (first newline not backslashed
  3820.      and not in a string or comment).
  3821.      Set COPY_DIRECTIVE if the directive must be copied
  3822.      (it contains a backslash-newline or a comment).  */
  3823.  
  3824.       buf = bp = after_ident;
  3825.       while (bp < limit) {
  3826.     register U_CHAR c = *bp++;
  3827.     switch (c) {
  3828.     case '\\':
  3829.       if (bp < limit) {
  3830.         if (*bp == '\n') {
  3831.           ip->lineno++;
  3832.           copy_directive = 1;
  3833.           bp++;
  3834.         } else if (traditional)
  3835.           bp++;
  3836.       }
  3837.       break;
  3838.  
  3839.     case '\'':
  3840.     case '\"':
  3841.       bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_directive, &unterminated);
  3842.       /* Don't bother calling the directive if we already got an error
  3843.          message due to unterminated string.  Skip everything and pretend
  3844.          we called the directive.  */
  3845.       if (unterminated) {
  3846.         if (traditional) {
  3847.           /* Traditional preprocessing permits unterminated strings.  */
  3848.           ip->bufp = bp;
  3849.           goto endloop1;
  3850.         }
  3851.         ip->bufp = bp;
  3852.         return 1;
  3853.       }
  3854.       break;
  3855.  
  3856.       /* <...> is special for #include.  */
  3857.     case '<':
  3858.       if (!kt->angle_brackets)
  3859.         break;
  3860.       while (bp < limit && *bp != '>' && *bp != '\n') {
  3861.         if (*bp == '\\' && bp[1] == '\n') {
  3862.           ip->lineno++;
  3863.           copy_directive = 1;
  3864.           bp++;
  3865.         }
  3866.         bp++;
  3867.       }
  3868.       break;
  3869.  
  3870.     case '/':
  3871.       if (*bp == '\\' && bp[1] == '\n')
  3872.         newline_fix (bp);
  3873.       if (*bp == '*'
  3874.           || (cplusplus_comments && *bp == '/')) {
  3875.         U_CHAR *obp = bp - 1;
  3876.         ip->bufp = bp + 1;
  3877.         skip_to_end_of_comment (ip, &ip->lineno, 0);
  3878.         bp = ip->bufp;
  3879.         /* No need to copy the directive because of a comment at the end;
  3880.            just don't include the comment in the directive.  */
  3881.         if (bp == limit || *bp == '\n') {
  3882.           bp = obp;
  3883.           goto endloop1;
  3884.         }
  3885.         /* Don't remove the comments if -traditional.  */
  3886.         if (! keep_comments)
  3887.           copy_directive++;
  3888.       }
  3889.       break;
  3890.  
  3891.     case '\f':
  3892.     case '\r':
  3893.     case '\v':
  3894.       if (pedantic)
  3895.         pedwarn ("%s in preprocessing directive", char_name[c]);
  3896.       break;
  3897.  
  3898.     case '\n':
  3899.       --bp;        /* Point to the newline */
  3900.       ip->bufp = bp;
  3901.       goto endloop1;
  3902.     }
  3903.       }
  3904.       ip->bufp = bp;
  3905.  
  3906.     endloop1:
  3907.       resume_p = ip->bufp;
  3908.       /* BP is the end of the directive.
  3909.      RESUME_P is the next interesting data after the directive.
  3910.      A comment may come between.  */
  3911.  
  3912.       /* If a directive should be copied through, and -E was given,
  3913.      pass it through before removing comments.  */
  3914.       if (!no_output && kt->pass_thru && put_out_comments) {
  3915.         int len;
  3916.  
  3917.     /* Output directive name.  */
  3918.         check_expand (op, kt->length + 2);
  3919.     /* Make sure # is at the start of a line */
  3920.     if (op->bufp > op->buf && op->bufp[-1] != '\n') {
  3921.       op->lineno++;
  3922.       *op->bufp++ = '\n';
  3923.     }
  3924.         *op->bufp++ = '#';
  3925.         bcopy (kt->name, op->bufp, kt->length);
  3926.         op->bufp += kt->length;
  3927.  
  3928.     /* Output arguments.  */
  3929.     len = (bp - buf);
  3930.     check_expand (op, len);
  3931.     bcopy (buf, (char *) op->bufp, len);
  3932.     op->bufp += len;
  3933.     /* Take account of any (escaped) newlines just output.  */
  3934.     while (--len >= 0)
  3935.       if (buf[len] == '\n')
  3936.         op->lineno++;
  3937.  
  3938.     already_output = &junk;
  3939.       }                /* Don't we need a newline or #line? */
  3940.  
  3941.       if (copy_directive) {
  3942.     register U_CHAR *xp = buf;
  3943.     /* Need to copy entire directive into temp buffer before dispatching */
  3944.  
  3945.     cp = (U_CHAR *) alloca (bp - buf + 5); /* room for directive plus
  3946.                           some slop */
  3947.     buf = cp;
  3948.  
  3949.     /* Copy to the new buffer, deleting comments
  3950.        and backslash-newlines (and whitespace surrounding the latter).  */
  3951.  
  3952.     while (xp < bp) {
  3953.       register U_CHAR c = *xp++;
  3954.       *cp++ = c;
  3955.  
  3956.       switch (c) {
  3957.       case '\n':
  3958.         abort ();  /* A bare newline should never part of the line.  */
  3959.         break;
  3960.  
  3961.         /* <...> is special for #include.  */
  3962.       case '<':
  3963.         if (!kt->angle_brackets)
  3964.           break;
  3965.         while (xp < bp && c != '>') {
  3966.           c = *xp++;
  3967.           if (c == '\\' && xp < bp && *xp == '\n')
  3968.         xp++;
  3969.           else
  3970.         *cp++ = c;
  3971.         }
  3972.         break;
  3973.  
  3974.       case '\\':
  3975.         if (*xp == '\n') {
  3976.           xp++;
  3977.           cp--;
  3978.           if (cp != buf && is_space[cp[-1]]) {
  3979.         while (cp != buf && is_space[cp[-1]]) cp--;
  3980.         cp++;
  3981.         SKIP_WHITE_SPACE (xp);
  3982.           } else if (is_space[*xp]) {
  3983.         *cp++ = *xp++;
  3984.         SKIP_WHITE_SPACE (xp);
  3985.           }
  3986.         } else if (traditional && xp < bp) {
  3987.           *cp++ = *xp++;
  3988.         }
  3989.         break;
  3990.  
  3991.       case '\'':
  3992.       case '\"':
  3993.         {
  3994.           register U_CHAR *bp1
  3995.         = skip_quoted_string (xp - 1, bp, ip->lineno,
  3996.                       NULL_PTR, NULL_PTR, NULL_PTR);
  3997.           while (xp != bp1)
  3998.         if (*xp == '\\') {
  3999.           if (*++xp != '\n')
  4000.             *cp++ = '\\';
  4001.           else
  4002.             xp++;
  4003.         } else
  4004.           *cp++ = *xp++;
  4005.         }
  4006.         break;
  4007.  
  4008.       case '/':
  4009.         if (*xp == '*'
  4010.         || (cplusplus_comments && *xp == '/')) {
  4011.           ip->bufp = xp + 1;
  4012.           /* If we already copied the directive through,
  4013.          already_output != 0 prevents outputting comment now.  */
  4014.           skip_to_end_of_comment (ip, already_output, 0);
  4015.           if (keep_comments)
  4016.         while (xp != ip->bufp)
  4017.           *cp++ = *xp++;
  4018.           /* Delete or replace the slash.  */
  4019.           else if (traditional)
  4020.         cp--;
  4021.           else
  4022.         cp[-1] = ' ';
  4023.           xp = ip->bufp;
  4024.         }
  4025.       }
  4026.     }
  4027.  
  4028.     /* Null-terminate the copy.  */
  4029.  
  4030.     *cp = 0;
  4031.       } else
  4032.     cp = bp;
  4033.  
  4034.       ip->bufp = resume_p;
  4035.  
  4036.       /* Some directives should be written out for cc1 to process,
  4037.      just as if they were not defined.  And sometimes we're copying
  4038.      definitions through.  */
  4039.  
  4040.       if (!no_output && already_output == 0
  4041.       && (kt->pass_thru
  4042.           || (kt->type == T_DEFINE
  4043.           && (dump_macros == dump_names
  4044.               || dump_macros == dump_definitions)))) {
  4045.         int len;
  4046.  
  4047.     /* Output directive name.  */
  4048.         check_expand (op, kt->length + 1);
  4049.         *op->bufp++ = '#';
  4050.         bcopy (kt->name, (char *) op->bufp, kt->length);
  4051.         op->bufp += kt->length;
  4052.  
  4053.     if (kt->pass_thru || dump_macros == dump_definitions) {
  4054.       /* Output arguments.  */
  4055.       len = (cp - buf);
  4056.       check_expand (op, len);
  4057.       bcopy (buf, (char *) op->bufp, len);
  4058.       op->bufp += len;
  4059.     } else if (kt->type == T_DEFINE && dump_macros == dump_names) {
  4060.       U_CHAR *xp = buf;
  4061.       U_CHAR *yp;
  4062.       SKIP_WHITE_SPACE (xp);
  4063.       yp = xp;
  4064.       while (is_idchar[*xp]) xp++;
  4065.       len = (xp - yp);
  4066.       check_expand (op, len + 1);
  4067.       *op->bufp++ = ' ';
  4068.       bcopy (yp, op->bufp, len);
  4069.       op->bufp += len;
  4070.     }
  4071.       }                /* Don't we need a newline or #line? */
  4072.  
  4073.       /* Call the appropriate directive handler.  buf now points to
  4074.      either the appropriate place in the input buffer, or to
  4075.      the temp buffer if it was necessary to make one.  cp
  4076.      points to the first char after the contents of the (possibly
  4077.      copied) directive, in either case. */
  4078.       (*kt->func) (buf, cp, op, kt);
  4079.       check_expand (op, ip->length - (ip->bufp - ip->buf));
  4080.  
  4081.       return 1;
  4082.     }
  4083.   }
  4084.  
  4085.   /* It is deliberate that we don't warn about undefined directives.
  4086.      That is the responsibility of cc1.  */
  4087.   return 0;
  4088. }
  4089.  
  4090. static struct tm *
  4091. timestamp ()
  4092. {
  4093.   static struct tm *timebuf;
  4094.   if (!timebuf) {
  4095.     time_t t = time ((time_t *)0);
  4096.     timebuf = localtime (&t);
  4097.   }
  4098.   return timebuf;
  4099. }
  4100.  
  4101. static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  4102.                  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  4103.                 };
  4104.  
  4105. /*
  4106.  * expand things like __FILE__.  Place the expansion into the output
  4107.  * buffer *without* rescanning.
  4108.  */
  4109.  
  4110. static void
  4111. special_symbol (hp, op)
  4112.      HASHNODE *hp;
  4113.      FILE_BUF *op;
  4114. {
  4115.   char *buf;
  4116.   int i, len;
  4117.   int true_indepth;
  4118.   FILE_BUF *ip = NULL;
  4119.   struct tm *timebuf;
  4120.  
  4121.   int paren = 0;        /* For special `defined' keyword */
  4122.  
  4123.   if (pcp_outfile && pcp_inside_if
  4124.       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
  4125.     error ("Predefined macro `%s' used inside `#if' during precompilation",
  4126.        hp->name);
  4127.     
  4128.   for (i = indepth; i >= 0; i--)
  4129.     if (instack[i].fname != NULL) {
  4130.       ip = &instack[i];
  4131.       break;
  4132.     }
  4133.   if (ip == NULL) {
  4134.     error ("cccp error: not in any file?!");
  4135.     return;            /* the show must go on */
  4136.   }
  4137.  
  4138.   switch (hp->type) {
  4139.   case T_FILE:
  4140.   case T_BASE_FILE:
  4141.     {
  4142.       char *string;
  4143.       if (hp->type == T_FILE)
  4144.     string = ip->nominal_fname;
  4145.       else
  4146.     string = instack[0].nominal_fname;
  4147.  
  4148.       if (string)
  4149.     {
  4150.       buf = (char *) alloca (3 + 4 * strlen (string));
  4151.       quote_string (buf, string);
  4152.     }
  4153.       else
  4154.     buf = "\"\"";
  4155.  
  4156.       break;
  4157.     }
  4158.  
  4159.   case T_INCLUDE_LEVEL:
  4160.     true_indepth = 0;
  4161.     for (i = indepth; i >= 0; i--)
  4162.       if (instack[i].fname != NULL)
  4163.         true_indepth++;
  4164.  
  4165.     buf = (char *) alloca (8);    /* Eight bytes ought to be more than enough */
  4166.     sprintf (buf, "%d", true_indepth - 1);
  4167.     break;
  4168.  
  4169.   case T_VERSION:
  4170.     buf = (char *) alloca (3 + strlen (version_string));
  4171.     sprintf (buf, "\"%s\"", version_string);
  4172.     break;
  4173.  
  4174. #ifndef NO_BUILTIN_SIZE_TYPE
  4175.   case T_SIZE_TYPE:
  4176.     buf = SIZE_TYPE;
  4177.     break;
  4178. #endif
  4179.  
  4180. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  4181.   case T_PTRDIFF_TYPE:
  4182.     buf = PTRDIFF_TYPE;
  4183.     break;
  4184. #endif
  4185.  
  4186.   case T_WCHAR_TYPE:
  4187.     buf = wchar_type;
  4188.     break;
  4189.  
  4190.   case T_USER_LABEL_PREFIX_TYPE:
  4191.     buf = USER_LABEL_PREFIX;
  4192.     break;
  4193.  
  4194.   case T_REGISTER_PREFIX_TYPE:
  4195.     buf = REGISTER_PREFIX;
  4196.     break;
  4197.  
  4198.   case T_IMMEDIATE_PREFIX_TYPE:
  4199.     buf = IMMEDIATE_PREFIX;
  4200.     break;
  4201.  
  4202.   case T_CONST:
  4203.     buf = hp->value.cpval;
  4204.     if (pcp_inside_if && pcp_outfile)
  4205.       /* Output a precondition for this macro use */
  4206.       fprintf (pcp_outfile, "#define %s %s\n", hp->name, buf);
  4207.     break;
  4208.  
  4209.   case T_SPECLINE:
  4210.     buf = (char *) alloca (10);
  4211.     sprintf (buf, "%d", ip->lineno);
  4212.     break;
  4213.  
  4214.   case T_DATE:
  4215.   case T_TIME:
  4216.     buf = (char *) alloca (20);
  4217.     timebuf = timestamp ();
  4218.     if (hp->type == T_DATE)
  4219.       sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
  4220.           timebuf->tm_mday, timebuf->tm_year + 1900);
  4221.     else
  4222.       sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
  4223.           timebuf->tm_sec);
  4224.     break;
  4225.  
  4226.   case T_SPEC_DEFINED:
  4227.     buf = " 0 ";        /* Assume symbol is not defined */
  4228.     ip = &instack[indepth];
  4229.     SKIP_WHITE_SPACE (ip->bufp);
  4230.     if (*ip->bufp == '(') {
  4231.       paren++;
  4232.       ip->bufp++;            /* Skip over the paren */
  4233.       SKIP_WHITE_SPACE (ip->bufp);
  4234.     }
  4235.  
  4236.     if (!is_idstart[*ip->bufp])
  4237.       goto oops;
  4238.     if ((hp = lookup (ip->bufp, -1, -1))) {
  4239.       if (pcp_outfile && pcp_inside_if
  4240.       && (hp->type == T_CONST
  4241.           || (hp->type == T_MACRO && hp->value.defn->predefined)))
  4242.     /* Output a precondition for this macro use. */
  4243.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  4244.       buf = " 1 ";
  4245.     }
  4246.     else
  4247.       if (pcp_outfile && pcp_inside_if)    {
  4248.     /* Output a precondition for this macro use */
  4249.     U_CHAR *cp = ip->bufp;
  4250.     fprintf (pcp_outfile, "#undef ");
  4251.     while (is_idchar[*cp]) /* Ick! */
  4252.       fputc (*cp++, pcp_outfile);
  4253.     putc ('\n', pcp_outfile);
  4254.       }
  4255.     while (is_idchar[*ip->bufp])
  4256.       ++ip->bufp;
  4257.     SKIP_WHITE_SPACE (ip->bufp);
  4258.     if (paren) {
  4259.       if (*ip->bufp != ')')
  4260.     goto oops;
  4261.       ++ip->bufp;
  4262.     }
  4263.     break;
  4264.  
  4265. oops:
  4266.  
  4267.     error ("`defined' without an identifier");
  4268.     break;
  4269.  
  4270.   default:
  4271.     error ("cccp error: invalid special hash type"); /* time for gdb */
  4272.     abort ();
  4273.   }
  4274.   len = strlen (buf);
  4275.   check_expand (op, len);
  4276.   bcopy (buf, (char *) op->bufp, len);
  4277.   op->bufp += len;
  4278.  
  4279.   return;
  4280. }
  4281.  
  4282.  
  4283. /* Routines to handle #directives */
  4284.  
  4285. /* Handle #include and #import.
  4286.    This function expects to see "fname" or <fname> on the input.  */
  4287.  
  4288. static int
  4289. do_include (buf, limit, op, keyword)
  4290.      U_CHAR *buf, *limit;
  4291.      FILE_BUF *op;
  4292.      struct directive *keyword;
  4293. {
  4294.   int importing = (keyword->type == T_IMPORT);
  4295.   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
  4296.   static int import_warning = 0;
  4297.   char *fname;        /* Dynamically allocated fname buffer */
  4298.   char *pcftry;
  4299.   char *pcfname;
  4300.   U_CHAR *fbeg, *fend;        /* Beginning and end of fname */
  4301.  
  4302.   struct file_name_list *search_start = include; /* Chain of dirs to search */
  4303.   struct file_name_list dsp[1];    /* First in chain, if #include "..." */
  4304.   struct file_name_list *searchptr = 0;
  4305.   int flen;
  4306.  
  4307.   int f;            /* file number */
  4308.  
  4309.   int retried = 0;        /* Have already tried macro
  4310.                    expanding the include line*/
  4311.   int angle_brackets = 0;    /* 0 for "...", 1 for <...> */
  4312.   int pcf = -1;
  4313.   char *pcfbuf;
  4314.   char *pcfbuflimit;
  4315.   int pcfnum;
  4316.   f= -1;            /* JF we iz paranoid! */
  4317.  
  4318.   if (importing && warn_import && !inhibit_warnings
  4319.       && !instack[indepth].system_header_p && !import_warning) {
  4320.     import_warning = 1;
  4321.     warning ("using `#import' is not recommended");
  4322.     fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
  4323.     fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
  4324.     fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
  4325.     fprintf (stderr, "  #ifndef _FOO_H_INCLUDED\n");
  4326.     fprintf (stderr, "  #define _FOO_H_INCLUDED\n");
  4327.     fprintf (stderr, "  ... <real contents of file> ...\n");
  4328.     fprintf (stderr, "  #endif /* Not _FOO_H_INCLUDED */\n\n");
  4329.     fprintf (stderr, "Then users can use `#include' any number of times.\n");
  4330.     fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
  4331.     fprintf (stderr, "when it is equipped with such a conditional.\n");
  4332.   }
  4333.  
  4334. get_filename:
  4335.  
  4336.   fbeg = buf;
  4337.   SKIP_WHITE_SPACE (fbeg);
  4338.   /* Discard trailing whitespace so we can easily see
  4339.      if we have parsed all the significant chars we were given.  */
  4340.   while (limit != fbeg && is_hor_space[limit[-1]]) limit--;
  4341.  
  4342.   switch (*fbeg++) {
  4343.   case '\"':
  4344.     {
  4345.       FILE_BUF *fp;
  4346.       /* Copy the operand text, concatenating the strings.  */
  4347.       {
  4348.     U_CHAR *fin = fbeg;
  4349.     fbeg = (U_CHAR *) alloca (limit - fbeg + 1);
  4350.     fend = fbeg;
  4351.     while (fin != limit) {
  4352.       while (fin != limit && *fin != '\"')
  4353.         *fend++ = *fin++;
  4354.       fin++;
  4355.       if (fin == limit)
  4356.         break;
  4357.       /* If not at the end, there had better be another string.  */
  4358.       /* Skip just horiz space, and don't go past limit.  */
  4359.       while (fin != limit && is_hor_space[*fin]) fin++;
  4360.       if (fin != limit && *fin == '\"')
  4361.         fin++;
  4362.       else
  4363.         goto fail;
  4364.     }
  4365.       }
  4366.       *fend = 0;
  4367.  
  4368.       /* We have "filename".  Figure out directory this source
  4369.      file is coming from and put it on the front of the list. */
  4370.  
  4371.       /* If -I- was specified, don't search current dir, only spec'd ones. */
  4372.       if (ignore_srcdir) break;
  4373.  
  4374.       for (fp = &instack[indepth]; fp >= instack; fp--)
  4375.     {
  4376.       int n;
  4377.       char *ep,*nam;
  4378.  
  4379.       if ((nam = fp->nominal_fname) != NULL) {
  4380.         /* Found a named file.  Figure out dir of the file,
  4381.            and put it in front of the search list.  */
  4382.         dsp[0].next = search_start;
  4383.         search_start = dsp;
  4384. #ifndef VMS
  4385.         ep = rindex (nam, '/');
  4386. #ifdef DIR_SEPARATOR
  4387.         if (ep == NULL) ep = rindex (nam, DIR_SEPARATOR);
  4388.         else {
  4389.           char *tmp = rindex (nam, DIR_SEPARATOR);
  4390.           if (tmp != NULL && tmp > ep) ep = tmp;
  4391.         }
  4392. #endif
  4393. #else                /* VMS */
  4394.         ep = rindex (nam, ']');
  4395.         if (ep == NULL) ep = rindex (nam, '>');
  4396.         if (ep == NULL) ep = rindex (nam, ':');
  4397.         if (ep != NULL) ep++;
  4398. #endif                /* VMS */
  4399.         if (ep != NULL) {
  4400.           n = ep - nam;
  4401.           dsp[0].fname = (char *) alloca (n + 1);
  4402.           strncpy (dsp[0].fname, nam, n);
  4403.           dsp[0].fname[n] = '\0';
  4404.           if (n + INCLUDE_LEN_FUDGE > max_include_len)
  4405.         max_include_len = n + INCLUDE_LEN_FUDGE;
  4406.         } else {
  4407.           dsp[0].fname = 0; /* Current directory */
  4408.         }
  4409.         dsp[0].got_name_map = 0;
  4410.         break;
  4411.       }
  4412.     }
  4413.       break;
  4414.     }
  4415.  
  4416.   case '<':
  4417.     fend = fbeg;
  4418.     while (fend != limit && *fend != '>') fend++;
  4419.     if (*fend == '>' && fend + 1 == limit) {
  4420.       angle_brackets = 1;
  4421.       /* If -I-, start with the first -I dir after the -I-.  */
  4422.       if (first_bracket_include)
  4423.     search_start = first_bracket_include;
  4424.       break;
  4425.     }
  4426.     goto fail;
  4427.  
  4428.   default:
  4429. #ifdef VMS
  4430.     /*
  4431.      * Support '#include xyz' like VAX-C to allow for easy use of all the
  4432.      * decwindow include files. It defaults to '#include <xyz.h>' (so the
  4433.      * code from case '<' is repeated here) and generates a warning.
  4434.      * (Note: macro expansion of `xyz' takes precedence.)
  4435.      */
  4436.     if (retried && isalpha(*(--fbeg))) {
  4437.       fend = fbeg;
  4438.       while (fend != limit && (!isspace(*fend))) fend++;
  4439.       warning ("VAX-C-style include specification found, use '#include <filename.h>' !");
  4440.       if (fend  == limit) {
  4441.     angle_brackets = 1;
  4442.     /* If -I-, start with the first -I dir after the -I-.  */
  4443.     if (first_bracket_include)
  4444.       search_start = first_bracket_include;
  4445.     break;
  4446.       }
  4447.     }
  4448. #endif
  4449.  
  4450.   fail:
  4451.     if (retried) {
  4452.       error ("`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
  4453.       return 0;
  4454.     } else {
  4455.       /* Expand buffer and then remove any newline markers.
  4456.      We can't just tell expand_to_temp_buffer to omit the markers,
  4457.      since it would put extra spaces in include file names.  */
  4458.       FILE_BUF trybuf;
  4459.       U_CHAR *src;
  4460.       trybuf = expand_to_temp_buffer (buf, limit, 1, 0);
  4461.       src = trybuf.buf;
  4462.       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  4463.       limit = buf;
  4464.       while (src != trybuf.bufp) {
  4465.     switch ((*limit++ = *src++)) {
  4466.       case '\n':
  4467.         limit--;
  4468.         src++;
  4469.         break;
  4470.  
  4471.       case '\'':
  4472.       case '\"':
  4473.         {
  4474.           U_CHAR *src1 = skip_quoted_string (src - 1, trybuf.bufp, 0,
  4475.                          NULL_PTR, NULL_PTR, NULL_PTR);
  4476.           while (src != src1)
  4477.         *limit++ = *src++;
  4478.         }
  4479.         break;
  4480.     }
  4481.       }
  4482.       *limit = 0;
  4483.       free (trybuf.buf);
  4484.       retried++;
  4485.       goto get_filename;
  4486.     }
  4487.   }
  4488.  
  4489.   /* For #include_next, skip in the search path
  4490.      past the dir in which the containing file was found.  */
  4491.   if (skip_dirs) {
  4492.     FILE_BUF *fp;
  4493.     for (fp = &instack[indepth]; fp >= instack; fp--)
  4494.       if (fp->fname != NULL) {
  4495.     /* fp->dir is null if the containing file was specified
  4496.        with an absolute file name.  In that case, don't skip anything.  */
  4497.     if (fp->dir)
  4498.       search_start = fp->dir->next;
  4499.     break;
  4500.       }
  4501.   }
  4502.  
  4503.   flen = fend - fbeg;
  4504.  
  4505.   if (flen == 0)
  4506.     {
  4507.       error ("empty file name in `#%s'", keyword->name);
  4508.       return 0;
  4509.     }
  4510.  
  4511.   /* Allocate this permanently, because it gets stored in the definitions
  4512.      of macros.  */
  4513.   fname = xmalloc (max_include_len + flen + 4);
  4514.   /* + 2 above for slash and terminating null.  */
  4515.   /* + 2 added for '.h' on VMS (to support '#include filename') */
  4516.  
  4517.   /* If specified file name is absolute, just open it.  */
  4518.  
  4519.   if (*fbeg == '/'
  4520. #ifdef DIR_SEPARATOR
  4521.       || *fbeg == DIR_SEPARATOR
  4522. #endif
  4523.       ) {
  4524.     strncpy (fname, (char *) fbeg, flen);
  4525.     fname[flen] = 0;
  4526.     if (redundant_include_p (fname))
  4527.       return 0;
  4528.     if (importing)
  4529.       f = lookup_import (fname, NULL_PTR);
  4530.     else
  4531.       f = open_include_file (fname, NULL_PTR);
  4532.     if (f == -2)
  4533.       return 0;        /* Already included this file */
  4534.   } else {
  4535.     /* Search directory path, trying to open the file.
  4536.        Copy each filename tried into FNAME.  */
  4537.  
  4538.     for (searchptr = search_start; searchptr; searchptr = searchptr->next) {
  4539.       if (searchptr->fname) {
  4540.     /* The empty string in a search path is ignored.
  4541.        This makes it possible to turn off entirely
  4542.        a standard piece of the list.  */
  4543.     if (searchptr->fname[0] == 0)
  4544.       continue;
  4545.     strcpy (fname, searchptr->fname);
  4546.     strcat (fname, "/");
  4547.     fname[strlen (fname) + flen] = 0;
  4548.       } else {
  4549.     fname[0] = 0;
  4550.       }
  4551.       strncat (fname, (char *) fbeg, flen);
  4552. #ifdef VMS
  4553.       /* Change this 1/2 Unix 1/2 VMS file specification into a
  4554.          full VMS file specification */
  4555.       if (searchptr->fname && (searchptr->fname[0] != 0)) {
  4556.     /* Fix up the filename */
  4557.     hack_vms_include_specification (fname);
  4558.       } else {
  4559.           /* This is a normal VMS filespec, so use it unchanged.  */
  4560.     strncpy (fname, fbeg, flen);
  4561.     fname[flen] = 0;
  4562.     /* if it's '#include filename', add the missing .h */
  4563.     if (index(fname,'.')==NULL) {
  4564.       strcat (fname, ".h");
  4565.     }
  4566.       }
  4567. #endif /* VMS */
  4568.       /* ??? There are currently 3 separate mechanisms for avoiding processing
  4569.      of redundant include files: #import, #pragma once, and
  4570.      redundant_include_p.  It would be nice if they were unified.  */
  4571.       if (redundant_include_p (fname))
  4572.     return 0;
  4573.       if (importing)
  4574.     f = lookup_import (fname, searchptr);
  4575.       else
  4576.     f = open_include_file (fname, searchptr);
  4577.       if (f == -2)
  4578.     return 0;            /* Already included this file */
  4579. #ifdef EACCES
  4580.       else if (f == -1 && errno == EACCES)
  4581.     warning ("Header file %s exists, but is not readable", fname);
  4582. #endif
  4583.       if (f >= 0)
  4584.     break;
  4585.     }
  4586.   }
  4587.  
  4588.   if (f < 0) {
  4589.     /* A file that was not found.  */
  4590.  
  4591.     strncpy (fname, (char *) fbeg, flen);
  4592.     fname[flen] = 0;
  4593.     /* If generating dependencies and -MG was specified, we assume missing
  4594.        files are leaf files, living in the same directory as the source file
  4595.        or other similar place; these missing files may be generated from
  4596.        other files and may not exist yet (eg: y.tab.h).  */
  4597.     if (print_deps_missing_files
  4598.     && print_deps > (angle_brackets || (system_include_depth > 0)))
  4599.       {
  4600.     /* If it was requested as a system header file,
  4601.        then assume it belongs in the first place to look for such.  */
  4602.     if (angle_brackets)
  4603.       {
  4604.         for (searchptr = search_start; searchptr; searchptr = searchptr->next)
  4605.           {
  4606.         if (searchptr->fname)
  4607.           {
  4608.             char *p;
  4609.  
  4610.             if (searchptr->fname[0] == 0)
  4611.               continue;
  4612.             p = (char *) alloca (strlen (searchptr->fname)
  4613.                      + strlen (fname) + 2);
  4614.             strcpy (p, searchptr->fname);
  4615.             strcat (p, "/");
  4616.             strcat (p, fname);
  4617.             deps_output (p, ' ');
  4618.             break;
  4619.           }
  4620.           }
  4621.       }
  4622.     else
  4623.       {
  4624.         /* Otherwise, omit the directory, as if the file existed
  4625.            in the directory with the source.  */
  4626.         deps_output (fname, ' ');
  4627.       }
  4628.       }
  4629.     /* If -M was specified, and this header file won't be added to the
  4630.        dependency list, then don't count this as an error, because we can
  4631.        still produce correct output.  Otherwise, we can't produce correct
  4632.        output, because there may be dependencies we need inside the missing
  4633.        file, and we don't know what directory this missing file exists in.  */
  4634.     else if (print_deps
  4635.     && (print_deps <= (angle_brackets || (system_include_depth > 0))))
  4636.       warning ("No include path in which to find %s", fname);
  4637.     else if (search_start)
  4638.       error_from_errno (fname);
  4639.     else
  4640.       error ("No include path in which to find %s", fname);
  4641.   } else {
  4642.     /* Check to see if this include file is a once-only include file.
  4643.        If so, give up.  */
  4644.  
  4645.     struct file_name_list* ptr;
  4646.  
  4647.     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
  4648.       if (!strcmp (ptr->fname, fname)) {
  4649.     close (f);
  4650.         return 0;                /* This file was once'd. */
  4651.       }
  4652.     }
  4653.  
  4654.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  4655.       if (!strcmp (ptr->fname, fname))
  4656.         break;                /* This file was included before. */
  4657.     }
  4658.  
  4659.     if (ptr == 0) {
  4660.       /* This is the first time for this file.  */
  4661.       /* Add it to list of files included.  */
  4662.  
  4663.       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  4664.       ptr->control_macro = 0;
  4665.       ptr->c_system_include_path = 0;
  4666.       ptr->next = all_include_files;
  4667.       all_include_files = ptr;
  4668.       ptr->fname = savestring (fname);
  4669.       ptr->got_name_map = 0;
  4670.  
  4671.       /* For -M, add this file to the dependencies.  */
  4672.       if (print_deps > (angle_brackets || (system_include_depth > 0)))
  4673.     deps_output (fname, ' ');
  4674.     }   
  4675.  
  4676.     /* Handle -H option.  */
  4677.     if (print_include_names) {
  4678.       output_dots (stderr, indepth);
  4679.       fprintf (stderr, "%s\n", fname);
  4680.     }
  4681.  
  4682.     if (angle_brackets)
  4683.       system_include_depth++;
  4684.  
  4685.     /* Actually process the file.  */
  4686.     add_import (f, fname);    /* Record file on "seen" list for #import. */
  4687.  
  4688.     pcftry = (char *) alloca (strlen (fname) + 30);
  4689.     pcfbuf = 0;
  4690.     pcfnum = 0;
  4691.  
  4692.     if (!no_precomp)
  4693.       {
  4694.     struct stat stat_f;
  4695.  
  4696.     fstat (f, &stat_f);
  4697.  
  4698.     do {
  4699.       sprintf (pcftry, "%s%d", fname, pcfnum++);
  4700.  
  4701.       pcf = open (pcftry, O_RDONLY, 0666);
  4702.       if (pcf != -1)
  4703.         {
  4704.           struct stat s;
  4705.  
  4706.           fstat (pcf, &s);
  4707.           if (bcmp ((char *) &stat_f.st_ino, (char *) &s.st_ino,
  4708.             sizeof (s.st_ino))
  4709.           || stat_f.st_dev != s.st_dev)
  4710.         {
  4711.           pcfbuf = check_precompiled (pcf, fname, &pcfbuflimit);
  4712.           /* Don't need it any more.  */
  4713.           close (pcf);
  4714.         }
  4715.           else
  4716.         {
  4717.           /* Don't need it at all.  */
  4718.           close (pcf);
  4719.           break;
  4720.         }
  4721.         }
  4722.     } while (pcf != -1 && !pcfbuf);
  4723.       }
  4724.     
  4725.     /* Actually process the file */
  4726.     if (pcfbuf) {
  4727.       pcfname = xmalloc (strlen (pcftry) + 1);
  4728.       strcpy (pcfname, pcftry);
  4729.       pcfinclude ((U_CHAR *) pcfbuf, (U_CHAR *) pcfbuflimit,
  4730.           (U_CHAR *) fname, op);
  4731.     }
  4732.     else
  4733.       finclude (f, fname, op, is_system_include (fname), searchptr);
  4734.  
  4735.     if (angle_brackets)
  4736.       system_include_depth--;
  4737.   }
  4738.   return 0;
  4739. }
  4740.  
  4741. /* Return nonzero if there is no need to include file NAME
  4742.    because it has already been included and it contains a conditional
  4743.    to make a repeated include do nothing.  */
  4744.  
  4745. static int
  4746. redundant_include_p (name)
  4747.      char *name;
  4748. {
  4749.   struct file_name_list *l = all_include_files;
  4750.   for (; l; l = l->next)
  4751.     if (! strcmp (name, l->fname)
  4752.     && l->control_macro
  4753.     && lookup (l->control_macro, -1, -1))
  4754.       return 1;
  4755.   return 0;
  4756. }
  4757.  
  4758. /* Return nonzero if the given FILENAME is an absolute pathname which
  4759.    designates a file within one of the known "system" include file
  4760.    directories.  We assume here that if the given FILENAME looks like
  4761.    it is the name of a file which resides either directly in a "system"
  4762.    include file directory, or within any subdirectory thereof, then the
  4763.    given file must be a "system" include file.  This function tells us
  4764.    if we should suppress pedantic errors/warnings for the given FILENAME.
  4765.  
  4766.    The value is 2 if the file is a C-language system header file
  4767.    for which C++ should (on most systems) assume `extern "C"'.  */
  4768.  
  4769. static int
  4770. is_system_include (filename)
  4771.     register char *filename;
  4772. {
  4773.   struct file_name_list *searchptr;
  4774.  
  4775.   for (searchptr = first_system_include; searchptr;
  4776.        searchptr = searchptr->next)
  4777.     if (searchptr->fname) {
  4778.       register char *sys_dir = searchptr->fname;
  4779.       register unsigned length = strlen (sys_dir);
  4780.  
  4781.       if (! strncmp (sys_dir, filename, length)
  4782.       && (filename[length] == '/'
  4783. #ifdef DIR_SEPARATOR
  4784.           || filename[length] == DIR_SEPARATOR
  4785. #endif
  4786.           )) {
  4787.     if (searchptr->c_system_include_path)
  4788.       return 2;
  4789.     else
  4790.       return 1;
  4791.       }
  4792.     }
  4793.   return 0;
  4794. }
  4795.  
  4796. /* The file_name_map structure holds a mapping of file names for a
  4797.    particular directory.  This mapping is read from the file named
  4798.    FILE_NAME_MAP_FILE in that directory.  Such a file can be used to
  4799.    map filenames on a file system with severe filename restrictions,
  4800.    such as DOS.  The format of the file name map file is just a series
  4801.    of lines with two tokens on each line.  The first token is the name
  4802.    to map, and the second token is the actual name to use.  */
  4803.  
  4804. struct file_name_map
  4805. {
  4806.   struct file_name_map *map_next;
  4807.   char *map_from;
  4808.   char *map_to;
  4809. };
  4810.  
  4811. #define FILE_NAME_MAP_FILE "header.gcc"
  4812.  
  4813. /* Read a space delimited string of unlimited length from a stdio
  4814.    file.  */
  4815.  
  4816. static char *
  4817. read_filename_string (ch, f)
  4818.      int ch;
  4819.      FILE *f;
  4820. {
  4821.   char *alloc, *set;
  4822.   int len;
  4823.  
  4824.   len = 20;
  4825.   set = alloc = xmalloc (len + 1);
  4826.   if (! is_space[ch])
  4827.     {
  4828.       *set++ = ch;
  4829.       while ((ch = getc (f)) != EOF && ! is_space[ch])
  4830.     {
  4831.       if (set - alloc == len)
  4832.         {
  4833.           len *= 2;
  4834.           alloc = xrealloc (alloc, len + 1);
  4835.           set = alloc + len / 2;
  4836.         }
  4837.       *set++ = ch;
  4838.     }
  4839.     }
  4840.   *set = '\0';
  4841.   ungetc (ch, f);
  4842.   return alloc;
  4843. }
  4844.  
  4845. /* Read the file name map file for DIRNAME.  */
  4846.  
  4847. static struct file_name_map *
  4848. read_name_map (dirname)
  4849.      char *dirname;
  4850. {
  4851.   /* This structure holds a linked list of file name maps, one per
  4852.      directory.  */
  4853.   struct file_name_map_list
  4854.     {
  4855.       struct file_name_map_list *map_list_next;
  4856.       char *map_list_name;
  4857.       struct file_name_map *map_list_map;
  4858.     };
  4859.   static struct file_name_map_list *map_list;
  4860.   register struct file_name_map_list *map_list_ptr;
  4861.   char *name;
  4862.   FILE *f;
  4863.  
  4864.   for (map_list_ptr = map_list; map_list_ptr;
  4865.        map_list_ptr = map_list_ptr->map_list_next)
  4866.     if (! strcmp (map_list_ptr->map_list_name, dirname))
  4867.       return map_list_ptr->map_list_map;
  4868.  
  4869.   map_list_ptr = ((struct file_name_map_list *)
  4870.           xmalloc (sizeof (struct file_name_map_list)));
  4871.   map_list_ptr->map_list_name = savestring (dirname);
  4872.   map_list_ptr->map_list_map = NULL;
  4873.  
  4874.   name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
  4875.   strcpy (name, dirname);
  4876.   if (*dirname)
  4877.     strcat (name, "/");
  4878.   strcat (name, FILE_NAME_MAP_FILE);
  4879.   f = fopen (name, "r");
  4880.   if (!f)
  4881.     map_list_ptr->map_list_map = NULL;
  4882.   else
  4883.     {
  4884.       int ch;
  4885.       int dirlen = strlen (dirname);
  4886.  
  4887.       while ((ch = getc (f)) != EOF)
  4888.     {
  4889.       char *from, *to;
  4890.       struct file_name_map *ptr;
  4891.  
  4892.       if (is_space[ch])
  4893.         continue;
  4894.       from = read_filename_string (ch, f);
  4895.       while ((ch = getc (f)) != EOF && is_hor_space[ch])
  4896.         ;
  4897.       to = read_filename_string (ch, f);
  4898.  
  4899.       ptr = ((struct file_name_map *)
  4900.          xmalloc (sizeof (struct file_name_map)));
  4901.       ptr->map_from = from;
  4902.  
  4903.       /* Make the real filename absolute.  */
  4904.       if (*to == '/')
  4905.         ptr->map_to = to;
  4906.       else
  4907.         {
  4908.           ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
  4909.           strcpy (ptr->map_to, dirname);
  4910.           ptr->map_to[dirlen] = '/';
  4911.           strcpy (ptr->map_to + dirlen + 1, to);
  4912.           free (to);
  4913.         }          
  4914.  
  4915.       ptr->map_next = map_list_ptr->map_list_map;
  4916.       map_list_ptr->map_list_map = ptr;
  4917.  
  4918.       while ((ch = getc (f)) != '\n')
  4919.         if (ch == EOF)
  4920.           break;
  4921.     }
  4922.       fclose (f);
  4923.     }
  4924.   
  4925.   map_list_ptr->map_list_next = map_list;
  4926.   map_list = map_list_ptr;
  4927.  
  4928.   return map_list_ptr->map_list_map;
  4929. }  
  4930.  
  4931. /* Try to open include file FILENAME.  SEARCHPTR is the directory
  4932.    being tried from the include file search path.  This function maps
  4933.    filenames on file systems based on information read by
  4934.    read_name_map.  */
  4935.  
  4936. static int
  4937. open_include_file (filename, searchptr)
  4938.      char *filename;
  4939.      struct file_name_list *searchptr;
  4940. {
  4941.   register struct file_name_map *map;
  4942.   register char *from;
  4943.   char *p, *dir;
  4944.  
  4945.   if (searchptr && ! searchptr->got_name_map)
  4946.     {
  4947.       searchptr->name_map = read_name_map (searchptr->fname
  4948.                        ? searchptr->fname : ".");
  4949.       searchptr->got_name_map = 1;
  4950.     }
  4951.  
  4952.   /* First check the mapping for the directory we are using.  */
  4953.   if (searchptr && searchptr->name_map)
  4954.     {
  4955.       from = filename;
  4956.       if (searchptr->fname)
  4957.     from += strlen (searchptr->fname) + 1;
  4958.       for (map = searchptr->name_map; map; map = map->map_next)
  4959.     {
  4960.       if (! strcmp (map->map_from, from))
  4961.         {
  4962.           /* Found a match.  */
  4963.           return open (map->map_to, O_RDONLY, 0666);
  4964.         }
  4965.     }
  4966.     }
  4967.  
  4968.   /* Try to find a mapping file for the particular directory we are
  4969.      looking in.  Thus #include <sys/types.h> will look up sys/types.h
  4970.      in /usr/include/header.gcc and look up types.h in
  4971.      /usr/include/sys/header.gcc.  */
  4972.   p = rindex (filename, '/');
  4973. #ifdef DIR_SEPARATOR
  4974.   if (! p) p = rindex (filename, DIR_SEPARATOR);
  4975.   else {
  4976.     char *tmp = rindex (filename, DIR_SEPARATOR);
  4977.     if (tmp != NULL && tmp > p) p = tmp;
  4978.   }
  4979. #endif
  4980.   if (! p)
  4981.     p = filename;
  4982.   if (searchptr
  4983.       && searchptr->fname
  4984.       && strlen (searchptr->fname) == p - filename
  4985.       && ! strncmp (searchptr->fname, filename, p - filename))
  4986.     {
  4987.       /* FILENAME is in SEARCHPTR, which we've already checked.  */
  4988.       return open (filename, O_RDONLY, 0666);
  4989.     }
  4990.  
  4991.   if (p == filename)
  4992.     {
  4993.       dir = ".";
  4994.       from = filename;
  4995.     }
  4996.   else
  4997.     {
  4998.       dir = (char *) alloca (p - filename + 1);
  4999.       bcopy (filename, dir, p - filename);
  5000.       dir[p - filename] = '\0';
  5001.       from = p + 1;
  5002.     }
  5003.   for (map = read_name_map (dir); map; map = map->map_next)
  5004.     if (! strcmp (map->map_from, from))
  5005.       return open (map->map_to, O_RDONLY, 0666);
  5006.  
  5007.   return open (filename, O_RDONLY, 0666);
  5008. }
  5009.  
  5010. /* Process the contents of include file FNAME, already open on descriptor F,
  5011.    with output to OP.
  5012.    SYSTEM_HEADER_P is 1 if this file resides in any one of the known
  5013.    "system" include directories (as decided by the `is_system_include'
  5014.    function above).
  5015.    DIRPTR is the link in the dir path through which this file was found,
  5016.    or 0 if the file name was absolute.  */
  5017.  
  5018. static void
  5019. finclude (f, fname, op, system_header_p, dirptr)
  5020.      int f;
  5021.      char *fname;
  5022.      FILE_BUF *op;
  5023.      int system_header_p;
  5024.      struct file_name_list *dirptr;
  5025. {
  5026.   int st_mode;
  5027.   long st_size;
  5028.   long i;
  5029.   FILE_BUF *fp;            /* For input stack frame */
  5030.   int missing_newline = 0;
  5031.  
  5032.   CHECK_DEPTH (return;);
  5033.  
  5034.   if (file_size_and_mode (f, &st_mode, &st_size) < 0)
  5035.     {
  5036.       perror_with_name (fname);
  5037.       close (f);
  5038.       return;
  5039.     }
  5040.  
  5041.   fp = &instack[indepth + 1];
  5042.   bzero ((char *) fp, sizeof (FILE_BUF));
  5043.   fp->nominal_fname = fp->fname = fname;
  5044.   fp->length = 0;
  5045.   fp->lineno = 1;
  5046.   fp->if_stack = if_stack;
  5047.   fp->system_header_p = system_header_p;
  5048.   fp->dir = dirptr;
  5049.  
  5050.   if (S_ISREG (st_mode)) {
  5051.     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
  5052.     fp->bufp = fp->buf;
  5053.  
  5054.     /* Read the file contents, knowing that st_size is an upper bound
  5055.        on the number of bytes we can read.  */
  5056.     fp->length = safe_read (f, (char *) fp->buf, st_size);
  5057.     if (fp->length < 0) goto nope;
  5058.   }
  5059.   else if (S_ISDIR (st_mode)) {
  5060.     error ("directory `%s' specified in #include", fname);
  5061.     close (f);
  5062.     return;
  5063.   } else {
  5064.     /* Cannot count its file size before reading.
  5065.        First read the entire file into heap and
  5066.        copy them into buffer on stack. */
  5067.  
  5068.     int bsize = 2000;
  5069.  
  5070.     st_size = 0;
  5071.     fp->buf = (U_CHAR *) xmalloc (bsize + 2);
  5072.  
  5073.     for (;;) {
  5074.       i = safe_read (f, (char *) fp->buf + st_size, bsize - st_size);
  5075.       if (i < 0)
  5076.     goto nope;      /* error! */
  5077.       st_size += i;
  5078.       if (st_size != bsize)
  5079.     break;    /* End of file */
  5080.       bsize *= 2;
  5081.       fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
  5082.     }
  5083.     fp->bufp = fp->buf;
  5084.     fp->length = st_size;
  5085.   }
  5086.  
  5087.   if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
  5088.       /* Backslash-newline at end is not good enough.  */
  5089.       || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
  5090.     fp->buf[fp->length++] = '\n';
  5091.     missing_newline = 1;
  5092.   }
  5093.   fp->buf[fp->length] = '\0';
  5094.  
  5095.   /* Close descriptor now, so nesting does not use lots of descriptors.  */
  5096.   close (f);
  5097.  
  5098.   /* Must do this before calling trigraph_pcp, so that the correct file name
  5099.      will be printed in warning messages.  */
  5100.  
  5101.   indepth++;
  5102.   input_file_stack_tick++;
  5103.  
  5104.   if (no_msdoscr)
  5105.     msdos_pcp (fp);
  5106.   if (!no_trigraphs)
  5107.     trigraph_pcp (fp);
  5108.  
  5109.   output_line_directive (fp, op, 0, enter_file);
  5110.   rescan (op, 0);
  5111.  
  5112.   if (missing_newline)
  5113.     fp->lineno--;
  5114.  
  5115.   if (pedantic && missing_newline)
  5116.     pedwarn ("file does not end in newline");
  5117.  
  5118.   indepth--;
  5119.   input_file_stack_tick++;
  5120.   output_line_directive (&instack[indepth], op, 0, leave_file);
  5121.   free (fp->buf);
  5122.   return;
  5123.  
  5124.  nope:
  5125.  
  5126.   perror_with_name (fname);
  5127.   close (f);
  5128.   free (fp->buf);
  5129. }
  5130.  
  5131. /* Record that inclusion of the file named FILE
  5132.    should be controlled by the macro named MACRO_NAME.
  5133.    This means that trying to include the file again
  5134.    will do something if that macro is defined.  */
  5135.  
  5136. static void
  5137. record_control_macro (file, macro_name)
  5138.      char *file;
  5139.      U_CHAR *macro_name;
  5140. {
  5141.   struct file_name_list *new;
  5142.  
  5143.   for (new = all_include_files; new; new = new->next) {
  5144.     if (!strcmp (new->fname, file)) {
  5145.       new->control_macro = macro_name;
  5146.       return;
  5147.     }
  5148.   }
  5149.  
  5150.   /* If the file is not in all_include_files, something's wrong.  */
  5151.   abort ();
  5152. }
  5153.  
  5154. /* Maintain and search list of included files, for #import.  */
  5155.  
  5156. #define IMPORT_HASH_SIZE 31
  5157.  
  5158. struct import_file {
  5159.   char *name;
  5160.   ino_t inode;
  5161.   dev_t dev;
  5162.   struct import_file *next;
  5163. };
  5164.  
  5165. /* Hash table of files already included with #include or #import.  */
  5166.  
  5167. static struct import_file *import_hash_table[IMPORT_HASH_SIZE];
  5168.  
  5169. /* Hash a file name for import_hash_table.  */
  5170.  
  5171. static int 
  5172. import_hash (f)
  5173.      char *f;
  5174. {
  5175.   int val = 0;
  5176.  
  5177.   while (*f) val += *f++;
  5178.   return (val%IMPORT_HASH_SIZE);
  5179. }
  5180.  
  5181. /* Search for file FILENAME in import_hash_table.
  5182.    Return -2 if found, either a matching name or a matching inode.
  5183.    Otherwise, open the file and return a file descriptor if successful
  5184.    or -1 if unsuccessful.  */
  5185.  
  5186. static int
  5187. lookup_import (filename, searchptr)
  5188.      char *filename;
  5189.      struct file_name_list *searchptr;
  5190. {
  5191.   struct import_file *i;
  5192.   int h;
  5193.   int hashval;
  5194.   struct stat sb;
  5195.   int fd;
  5196.  
  5197.   hashval = import_hash (filename);
  5198.  
  5199.   /* Attempt to find file in list of already included files */
  5200.   i = import_hash_table[hashval];
  5201.  
  5202.   while (i) {
  5203.     if (!strcmp (filename, i->name))
  5204.       return -2;        /* return found */
  5205.     i = i->next;
  5206.   }
  5207.   /* Open it and try a match on inode/dev */
  5208.   fd = open_include_file (filename, searchptr);
  5209.   if (fd < 0)
  5210.     return fd;
  5211.   fstat (fd, &sb);
  5212.   for (h = 0; h < IMPORT_HASH_SIZE; h++) {
  5213.     i = import_hash_table[h];
  5214.     while (i) {
  5215.       /* Compare the inode and the device.
  5216.      Supposedly on some systems the inode is not a scalar.  */
  5217.       if (!bcmp ((char *) &i->inode, (char *) &sb.st_ino, sizeof (sb.st_ino))
  5218.       && i->dev == sb.st_dev) {
  5219.         close (fd);
  5220.         return -2;        /* return found */
  5221.       }
  5222.       i = i->next;
  5223.     }
  5224.   }
  5225.   return fd;            /* Not found, return open file */
  5226. }
  5227.  
  5228. /* Add the file FNAME, open on descriptor FD, to import_hash_table.  */
  5229.  
  5230. static void
  5231. add_import (fd, fname)
  5232.      int fd;
  5233.      char *fname;
  5234. {
  5235.   struct import_file *i;
  5236.   int hashval;
  5237.   struct stat sb;
  5238.  
  5239.   hashval = import_hash (fname);
  5240.   fstat (fd, &sb);
  5241.   i = (struct import_file *)xmalloc (sizeof (struct import_file));
  5242.   i->name = xmalloc (strlen (fname)+1);
  5243.   strcpy (i->name, fname);
  5244.   bcopy ((char *) &sb.st_ino, (char *) &i->inode, sizeof (sb.st_ino));
  5245.   i->dev = sb.st_dev;
  5246.   i->next = import_hash_table[hashval];
  5247.   import_hash_table[hashval] = i;
  5248. }
  5249.  
  5250. /* Load the specified precompiled header into core, and verify its
  5251.    preconditions.  PCF indicates the file descriptor to read, which must
  5252.    be a regular file.  FNAME indicates the file name of the original 
  5253.    header.  *LIMIT will be set to an address one past the end of the file.
  5254.    If the preconditions of the file are not satisfied, the buffer is 
  5255.    freed and we return 0.  If the preconditions are satisfied, return
  5256.    the address of the buffer following the preconditions.  The buffer, in
  5257.    this case, should never be freed because various pieces of it will
  5258.    be referred to until all precompiled strings are output at the end of
  5259.    the run.
  5260. */
  5261. static char *
  5262. check_precompiled (pcf, fname, limit)
  5263.      int pcf;
  5264.      char *fname;
  5265.      char **limit;
  5266. {
  5267.   int st_mode;
  5268.   long st_size;
  5269.   int length = 0;
  5270.   char *buf;
  5271.   char *cp;
  5272.  
  5273.   if (pcp_outfile)
  5274.     return 0;
  5275.   
  5276.   if (file_size_and_mode (pcf, &st_mode, &st_size) < 0)
  5277.     return 0;
  5278.  
  5279.   if (S_ISREG (st_mode))
  5280.     {
  5281.       buf = xmalloc (st_size + 2);
  5282.       length = safe_read (pcf, buf, st_size);
  5283.       if (length < 0)
  5284.     goto nope;
  5285.     }
  5286.   else
  5287.     abort ();
  5288.     
  5289.   if (length > 0 && buf[length-1] != '\n')
  5290.     buf[length++] = '\n';
  5291.   buf[length] = '\0';
  5292.   
  5293.   *limit = buf + length;
  5294.  
  5295.   /* File is in core.  Check the preconditions. */
  5296.   if (!check_preconditions (buf))
  5297.     goto nope;
  5298.   for (cp = buf; *cp; cp++)
  5299.     ;
  5300. #ifdef DEBUG_PCP
  5301.   fprintf (stderr, "Using preinclude %s\n", fname);
  5302. #endif
  5303.   return cp + 1;
  5304.  
  5305.  nope:
  5306. #ifdef DEBUG_PCP
  5307.   fprintf (stderr, "Cannot use preinclude %s\n", fname);
  5308. #endif
  5309.   free (buf);
  5310.   return 0;
  5311. }
  5312.  
  5313. /* PREC (null terminated) points to the preconditions of a
  5314.    precompiled header.  These are a series of #define and #undef
  5315.    lines which must match the current contents of the hash
  5316.    table.  */
  5317. static int 
  5318. check_preconditions (prec)
  5319.      char *prec;
  5320. {
  5321.   MACRODEF mdef;
  5322.   char *lineend;
  5323.   
  5324.   while (*prec) {
  5325.     lineend = index (prec, '\n');
  5326.     
  5327.     if (*prec++ != '#') {
  5328.       error ("Bad format encountered while reading precompiled file");
  5329.       return 0;
  5330.     }
  5331.     if (!strncmp (prec, "define", 6)) {
  5332.       HASHNODE *hp;
  5333.       
  5334.       prec += 6;
  5335.       mdef = create_definition ((U_CHAR *) prec, (U_CHAR *) lineend, NULL_PTR);
  5336.  
  5337.       if (mdef.defn == 0)
  5338.     abort ();
  5339.       
  5340.       if ((hp = lookup (mdef.symnam, mdef.symlen, -1)) == NULL
  5341.       || (hp->type != T_MACRO && hp->type != T_CONST)
  5342.       || (hp->type == T_MACRO
  5343.           && !compare_defs (mdef.defn, hp->value.defn)
  5344.           && (mdef.defn->length != 2
  5345.           || mdef.defn->expansion[0] != '\n'
  5346.           || mdef.defn->expansion[1] != ' ')))
  5347.     return 0;
  5348.     } else if (!strncmp (prec, "undef", 5)) {
  5349.       char *name;
  5350.       int len;
  5351.       
  5352.       prec += 5;
  5353.       while (is_hor_space[(U_CHAR) *prec])
  5354.     prec++;
  5355.       name = prec;
  5356.       while (is_idchar[(U_CHAR) *prec])
  5357.     prec++;
  5358.       len = prec - name;
  5359.       
  5360.       if (lookup ((U_CHAR *) name, len, -1))
  5361.     return 0;
  5362.     } else {
  5363.       error ("Bad format encountered while reading precompiled file");
  5364.       return 0;
  5365.     }
  5366.     prec = lineend + 1;
  5367.   }
  5368.   /* They all passed successfully */
  5369.   return 1;
  5370. }
  5371.  
  5372. /* Process the main body of a precompiled file.  BUF points to the
  5373.    string section of the file, following the preconditions.  LIMIT is one
  5374.    character past the end.  NAME is the name of the file being read
  5375.    in.  OP is the main output buffer */
  5376. static void
  5377. pcfinclude (buf, limit, name, op)
  5378.      U_CHAR *buf, *limit, *name;
  5379.      FILE_BUF *op;
  5380. {
  5381.   FILE_BUF tmpbuf;
  5382.   int nstrings;
  5383.   U_CHAR *cp = buf;
  5384.  
  5385.   /* First in the file comes 4 bytes indicating the number of strings, */
  5386.   /* in network byte order. (MSB first).  */
  5387.   nstrings = *cp++;
  5388.   nstrings = (nstrings << 8) | *cp++;
  5389.   nstrings = (nstrings << 8) | *cp++;
  5390.   nstrings = (nstrings << 8) | *cp++;
  5391.   
  5392.   /* Looping over each string... */
  5393.   while (nstrings--) {
  5394.     U_CHAR *string_start;
  5395.     U_CHAR *endofthiskey;
  5396.     STRINGDEF *str;
  5397.     int nkeys;
  5398.     
  5399.     /* Each string starts with a STRINGDEF structure (str), followed */
  5400.     /* by the text of the string (string_start) */
  5401.  
  5402.     /* First skip to a longword boundary */
  5403.     /* ??? Why a 4-byte boundary?  On all machines? */
  5404.     /* NOTE: This works correctly even if HOST_WIDE_INT
  5405.        is narrower than a pointer.
  5406.        Do not try risky measures here to get another type to use!
  5407.        Do not include stddef.h--it will fail!  */
  5408.     if ((HOST_WIDE_INT) cp & 3)
  5409.       cp += 4 - ((HOST_WIDE_INT) cp & 3);
  5410.     
  5411.     /* Now get the string. */
  5412.     str = (STRINGDEF *) (GENERIC_PTR) cp;
  5413.     string_start = cp += sizeof (STRINGDEF);
  5414.     
  5415.     for (; *cp; cp++)        /* skip the string */
  5416.       ;
  5417.     
  5418.     /* We need to macro expand the string here to ensure that the
  5419.        proper definition environment is in place.  If it were only
  5420.        expanded when we find out it is needed, macros necessary for
  5421.        its proper expansion might have had their definitions changed. */
  5422.     tmpbuf = expand_to_temp_buffer (string_start, cp++, 0, 0);
  5423.     /* Lineno is already set in the precompiled file */
  5424.     str->contents = tmpbuf.buf;
  5425.     str->len = tmpbuf.length;
  5426.     str->writeflag = 0;
  5427.     str->filename = name;
  5428.     str->output_mark = outbuf.bufp - outbuf.buf;
  5429.     
  5430.     str->chain = 0;
  5431.     *stringlist_tailp = str;
  5432.     stringlist_tailp = &str->chain;
  5433.     
  5434.     /* Next comes a fourbyte number indicating the number of keys */
  5435.     /* for this string. */
  5436.     nkeys = *cp++;
  5437.     nkeys = (nkeys << 8) | *cp++;
  5438.     nkeys = (nkeys << 8) | *cp++;
  5439.     nkeys = (nkeys << 8) | *cp++;
  5440.  
  5441.     /* If this number is -1, then the string is mandatory. */
  5442.     if (nkeys == -1)
  5443.       str->writeflag = 1;
  5444.     else
  5445.       /* Otherwise, for each key, */
  5446.       for (; nkeys--; free (tmpbuf.buf), cp = endofthiskey + 1) {
  5447.     KEYDEF *kp = (KEYDEF *) (GENERIC_PTR) cp;
  5448.     HASHNODE *hp;
  5449.     
  5450.     /* It starts with a KEYDEF structure */
  5451.     cp += sizeof (KEYDEF);
  5452.     
  5453.     /* Find the end of the key.  At the end of this for loop we
  5454.        advance CP to the start of the next key using this variable. */
  5455.     endofthiskey = cp + strlen ((char *) cp);
  5456.     kp->str = str;
  5457.     
  5458.     /* Expand the key, and enter it into the hash table. */
  5459.     tmpbuf = expand_to_temp_buffer (cp, endofthiskey, 0, 0);
  5460.     tmpbuf.bufp = tmpbuf.buf;
  5461.     
  5462.     while (is_hor_space[*tmpbuf.bufp])
  5463.       tmpbuf.bufp++;
  5464.     if (!is_idstart[*tmpbuf.bufp]
  5465.         || tmpbuf.bufp == tmpbuf.buf + tmpbuf.length) {
  5466.       str->writeflag = 1;
  5467.       continue;
  5468.     }
  5469.         
  5470.     hp = lookup (tmpbuf.bufp, -1, -1);
  5471.     if (hp == NULL) {
  5472.       kp->chain = 0;
  5473.       install (tmpbuf.bufp, -1, T_PCSTRING, (char *) kp, -1);
  5474.     }
  5475.     else if (hp->type == T_PCSTRING) {
  5476.       kp->chain = hp->value.keydef;
  5477.       hp->value.keydef = kp;
  5478.     }
  5479.     else
  5480.       str->writeflag = 1;
  5481.       }
  5482.   }
  5483.   /* This output_line_directive serves to switch us back to the current
  5484.      input file in case some of these strings get output (which will 
  5485.      result in line directives for the header file being output). */
  5486.   output_line_directive (&instack[indepth], op, 0, enter_file);
  5487. }
  5488.  
  5489. /* Called from rescan when it hits a key for strings.  Mark them all */
  5490.  /* used and clean up. */
  5491. static void
  5492. pcstring_used (hp)
  5493.      HASHNODE *hp;
  5494. {
  5495.   KEYDEF *kp;
  5496.   
  5497.   for (kp = hp->value.keydef; kp; kp = kp->chain)
  5498.     kp->str->writeflag = 1;
  5499.   delete_macro (hp);
  5500. }
  5501.  
  5502. /* Write the output, interspersing precompiled strings in their */
  5503.  /* appropriate places. */
  5504. static void
  5505. write_output ()
  5506. {
  5507.   STRINGDEF *next_string;
  5508.   U_CHAR *cur_buf_loc;
  5509.   int line_directive_len = 80;
  5510.   char *line_directive = xmalloc (line_directive_len);
  5511.   int len;
  5512.  
  5513.   /* In each run through the loop, either cur_buf_loc == */
  5514.   /* next_string_loc, in which case we print a series of strings, or */
  5515.   /* it is less than next_string_loc, in which case we write some of */
  5516.   /* the buffer. */
  5517.   cur_buf_loc = outbuf.buf; 
  5518.   next_string = stringlist;
  5519.   
  5520.   while (cur_buf_loc < outbuf.bufp || next_string) {
  5521.     if (next_string
  5522.     && cur_buf_loc - outbuf.buf == next_string->output_mark) {
  5523.       if (next_string->writeflag) {
  5524.     len = 4 * strlen ((char *) next_string->filename) + 32;
  5525.     while (len > line_directive_len)
  5526.       line_directive = xrealloc (line_directive, 
  5527.                      line_directive_len *= 2);
  5528.     sprintf (line_directive, "\n# %d ", next_string->lineno);
  5529.     strcpy (quote_string (line_directive + strlen (line_directive),
  5530.                   (char *) next_string->filename),
  5531.         "\n");
  5532.     safe_write (fileno (stdout), line_directive, strlen (line_directive));
  5533.     safe_write (fileno (stdout),
  5534.             (char *) next_string->contents, next_string->len);
  5535.       }          
  5536.       next_string = next_string->chain;
  5537.     }
  5538.     else {
  5539.       len = (next_string
  5540.          ? (next_string->output_mark 
  5541.         - (cur_buf_loc - outbuf.buf))
  5542.          : outbuf.bufp - cur_buf_loc);
  5543.       
  5544.       safe_write (fileno (stdout), (char *) cur_buf_loc, len);
  5545.       cur_buf_loc += len;
  5546.     }
  5547.   }
  5548.   free (line_directive);
  5549. }
  5550.  
  5551. /* Pass a directive through to the output file.
  5552.    BUF points to the contents of the directive, as a contiguous string.
  5553.    LIMIT points to the first character past the end of the directive.
  5554.    KEYWORD is the keyword-table entry for the directive.  */
  5555.  
  5556. static void
  5557. pass_thru_directive (buf, limit, op, keyword)
  5558.      U_CHAR *buf, *limit;
  5559.      FILE_BUF *op;
  5560.      struct directive *keyword;
  5561. {
  5562.   register unsigned keyword_length = keyword->length;
  5563.  
  5564.   check_expand (op, 1 + keyword_length + (limit - buf));
  5565.   *op->bufp++ = '#';
  5566.   bcopy (keyword->name, (char *) op->bufp, keyword_length);
  5567.   op->bufp += keyword_length;
  5568.   if (limit != buf && buf[0] != ' ')
  5569.     *op->bufp++ = ' ';
  5570.   bcopy ((char *) buf, (char *) op->bufp, limit - buf);
  5571.   op->bufp += (limit - buf);
  5572. #if 0
  5573.   *op->bufp++ = '\n';
  5574.   /* Count the line we have just made in the output,
  5575.      to get in sync properly.  */
  5576.   op->lineno++;
  5577. #endif
  5578. }
  5579.  
  5580. /* The arglist structure is built by do_define to tell
  5581.    collect_definition where the argument names begin.  That
  5582.    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
  5583.    would contain pointers to the strings x, y, and z.
  5584.    Collect_definition would then build a DEFINITION node,
  5585.    with reflist nodes pointing to the places x, y, and z had
  5586.    appeared.  So the arglist is just convenience data passed
  5587.    between these two routines.  It is not kept around after
  5588.    the current #define has been processed and entered into the
  5589.    hash table. */
  5590.  
  5591. struct arglist {
  5592.   struct arglist *next;
  5593.   U_CHAR *name;
  5594.   int length;
  5595.   int argno;
  5596.   char rest_args;
  5597. };
  5598.  
  5599. /* Create a DEFINITION node from a #define directive.  Arguments are 
  5600.    as for do_define. */
  5601. static MACRODEF
  5602. create_definition (buf, limit, op)
  5603.      U_CHAR *buf, *limit;
  5604.      FILE_BUF *op;
  5605. {
  5606.   U_CHAR *bp;            /* temp ptr into input buffer */
  5607.   U_CHAR *symname;        /* remember where symbol name starts */
  5608.   int sym_length;        /* and how long it is */
  5609.   int line = instack[indepth].lineno;
  5610.   char *file = instack[indepth].nominal_fname;
  5611.   int rest_args = 0;
  5612.  
  5613.   DEFINITION *defn;
  5614.   int arglengths = 0;        /* Accumulate lengths of arg names
  5615.                    plus number of args.  */
  5616.   MACRODEF mdef;
  5617.  
  5618.   bp = buf;
  5619.  
  5620.   while (is_hor_space[*bp])
  5621.     bp++;
  5622.  
  5623.   symname = bp;            /* remember where it starts */
  5624.   sym_length = check_macro_name (bp, "macro");
  5625.   bp += sym_length;
  5626.  
  5627.   /* Lossage will occur if identifiers or control keywords are broken
  5628.      across lines using backslash.  This is not the right place to take
  5629.      care of that. */
  5630.  
  5631.   if (*bp == '(') {
  5632.     struct arglist *arg_ptrs = NULL;
  5633.     int argno = 0;
  5634.  
  5635.     bp++;            /* skip '(' */
  5636.     SKIP_WHITE_SPACE (bp);
  5637.  
  5638.     /* Loop over macro argument names.  */
  5639.     while (*bp != ')') {
  5640.       struct arglist *temp;
  5641.  
  5642.       temp = (struct arglist *) alloca (sizeof (struct arglist));
  5643.       temp->name = bp;
  5644.       temp->next = arg_ptrs;
  5645.       temp->argno = argno++;
  5646.       temp->rest_args = 0;
  5647.       arg_ptrs = temp;
  5648.  
  5649.       if (rest_args)
  5650.     pedwarn ("another parameter follows `%s'",
  5651.          rest_extension);
  5652.  
  5653.       if (!is_idstart[*bp])
  5654.     pedwarn ("invalid character in macro parameter name");
  5655.       
  5656.       /* Find the end of the arg name.  */
  5657.       while (is_idchar[*bp]) {
  5658.     bp++;
  5659.     /* do we have a "special" rest-args extension here? */
  5660.     if (limit - bp > REST_EXTENSION_LENGTH &&
  5661.         bcmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
  5662.       rest_args = 1;
  5663.       temp->rest_args = 1;
  5664.       break;
  5665.     }
  5666.       }
  5667.       temp->length = bp - temp->name;
  5668.       if (rest_args == 1)
  5669.     bp += REST_EXTENSION_LENGTH;
  5670.       arglengths += temp->length + 2;
  5671.       SKIP_WHITE_SPACE (bp);
  5672.       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
  5673.     error ("badly punctuated parameter list in `#define'");
  5674.     goto nope;
  5675.       }
  5676.       if (*bp == ',') {
  5677.     bp++;
  5678.     SKIP_WHITE_SPACE (bp);
  5679.     /* A comma at this point can only be followed by an identifier.  */
  5680.     if (!is_idstart[*bp]) {
  5681.       error ("badly punctuated parameter list in `#define'");
  5682.       goto nope;
  5683.     }
  5684.       }
  5685.       if (bp >= limit) {
  5686.     error ("unterminated parameter list in `#define'");
  5687.     goto nope;
  5688.       }
  5689.       {
  5690.     struct arglist *otemp;
  5691.  
  5692.     for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
  5693.       if (temp->length == otemp->length &&
  5694.           bcmp (temp->name, otemp->name, temp->length) == 0) {
  5695.           error ("duplicate argument name `%.*s' in `#define'",
  5696.              temp->length, temp->name);
  5697.           goto nope;
  5698.       }
  5699.       }
  5700.     }
  5701.  
  5702.     ++bp;            /* skip paren */
  5703.     SKIP_WHITE_SPACE (bp);
  5704.     /* now everything from bp before limit is the definition. */
  5705.     defn = collect_expansion (bp, limit, argno, arg_ptrs);
  5706.     defn->rest_args = rest_args;
  5707.  
  5708.     /* Now set defn->args.argnames to the result of concatenating
  5709.        the argument names in reverse order
  5710.        with comma-space between them.  */
  5711.     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
  5712.     {
  5713.       struct arglist *temp;
  5714.       int i = 0;
  5715.       for (temp = arg_ptrs; temp; temp = temp->next) {
  5716.     bcopy (temp->name, &defn->args.argnames[i], temp->length);
  5717.     i += temp->length;
  5718.     if (temp->next != 0) {
  5719.       defn->args.argnames[i++] = ',';
  5720.       defn->args.argnames[i++] = ' ';
  5721.     }
  5722.       }
  5723.       defn->args.argnames[i] = 0;
  5724.     }
  5725.   } else {
  5726.     /* Simple expansion or empty definition.  */
  5727.  
  5728.     if (bp < limit)
  5729.       {
  5730.     if (is_hor_space[*bp]) {
  5731.       bp++;
  5732.       SKIP_WHITE_SPACE (bp);
  5733.     } else {
  5734.       switch (*bp) {
  5735.         case '!':  case '"':  case '#':  case '%':  case '&':  case '\'':
  5736.         case ')':  case '*':  case '+':  case ',':  case '-':  case '.':
  5737.         case '/':  case ':':  case ';':  case '<':  case '=':  case '>':
  5738.         case '?':  case '[':  case '\\': case ']':  case '^':  case '{':
  5739.         case '|':  case '}':  case '~':
  5740.           warning ("missing white space after `#define %.*s'",
  5741.                sym_length, symname);
  5742.           break;
  5743.  
  5744.         default:
  5745.           pedwarn ("missing white space after `#define %.*s'",
  5746.                sym_length, symname);
  5747.           break;
  5748.       }
  5749.     }
  5750.       }
  5751.     /* Now everything from bp before limit is the definition. */
  5752.     defn = collect_expansion (bp, limit, -1, NULL_PTR);
  5753.     defn->args.argnames = (U_CHAR *) "";
  5754.   }
  5755.  
  5756.   defn->line = line;
  5757.   defn->file = file;
  5758.  
  5759.   /* OP is null if this is a predefinition */
  5760.   defn->predefined = !op;
  5761.   mdef.defn = defn;
  5762.   mdef.symnam = symname;
  5763.   mdef.symlen = sym_length;
  5764.  
  5765.   return mdef;
  5766.  
  5767.  nope:
  5768.   mdef.defn = 0;
  5769.   return mdef;
  5770. }
  5771.  
  5772. /* Process a #define directive.
  5773. BUF points to the contents of the #define directive, as a contiguous string.
  5774. LIMIT points to the first character past the end of the definition.
  5775. KEYWORD is the keyword-table entry for #define.  */
  5776.  
  5777. static int
  5778. do_define (buf, limit, op, keyword)
  5779.      U_CHAR *buf, *limit;
  5780.      FILE_BUF *op;
  5781.      struct directive *keyword;
  5782. {
  5783.   int hashcode;
  5784.   MACRODEF mdef;
  5785.  
  5786.   /* If this is a precompiler run (with -pcp) pass thru #define directives.  */
  5787.   if (pcp_outfile && op)
  5788.     pass_thru_directive (buf, limit, op, keyword);
  5789.  
  5790.   mdef = create_definition (buf, limit, op);
  5791.   if (mdef.defn == 0)
  5792.     goto nope;
  5793.  
  5794.   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
  5795.  
  5796.   {
  5797.     HASHNODE *hp;
  5798.     if ((hp = lookup (mdef.symnam, mdef.symlen, hashcode)) != NULL) {
  5799.       int ok = 0;
  5800.       /* Redefining a precompiled key is ok.  */
  5801.       if (hp->type == T_PCSTRING)
  5802.     ok = 1;
  5803.       /* Redefining a macro is ok if the definitions are the same.  */
  5804.       else if (hp->type == T_MACRO)
  5805.     ok = ! compare_defs (mdef.defn, hp->value.defn);
  5806.       /* Redefining a constant is ok with -D.  */
  5807.       else if (hp->type == T_CONST)
  5808.         ok = ! done_initializing;
  5809.       /* Print the warning if it's not ok.  */
  5810.       if (!ok) {
  5811.         /* If we are passing through #define and #undef directives, do
  5812.        that for this re-definition now.  */
  5813.         if (debug_output && op)
  5814.       pass_thru_directive (buf, limit, op, keyword);
  5815.  
  5816.     pedwarn ("`%.*s' redefined", mdef.symlen, mdef.symnam);
  5817.     if (hp->type == T_MACRO)
  5818.       pedwarn_with_file_and_line (hp->value.defn->file, hp->value.defn->line,
  5819.                       "this is the location of the previous definition");
  5820.       }
  5821.       /* Replace the old definition.  */
  5822.       hp->type = T_MACRO;
  5823.       hp->value.defn = mdef.defn;
  5824.     } else {
  5825.       /* If we are passing through #define and #undef directives, do
  5826.      that for this new definition now.  */
  5827.       if (debug_output && op)
  5828.     pass_thru_directive (buf, limit, op, keyword);
  5829.       install (mdef.symnam, mdef.symlen, T_MACRO,
  5830.            (char *) mdef.defn, hashcode);
  5831.     }
  5832.   }
  5833.  
  5834.   return 0;
  5835.  
  5836. nope:
  5837.  
  5838.   return 1;
  5839. }
  5840.  
  5841. /* Check a purported macro name SYMNAME, and yield its length.
  5842.    USAGE is the kind of name this is intended for.  */
  5843.  
  5844. static int
  5845. check_macro_name (symname, usage)
  5846.      U_CHAR *symname;
  5847.      char *usage;
  5848. {
  5849.   U_CHAR *p;
  5850.   int sym_length;
  5851.  
  5852.   for (p = symname; is_idchar[*p]; p++)
  5853.     ;
  5854.   sym_length = p - symname;
  5855.   if (sym_length == 0)
  5856.     error ("invalid %s name", usage);
  5857.   else if (!is_idstart[*symname]
  5858.        || (sym_length == 7 && ! bcmp (symname, "defined", 7)))
  5859.     error ("invalid %s name `%.*s'", usage, sym_length, symname);
  5860.   return sym_length;
  5861. }
  5862.  
  5863. /*
  5864.  * return zero if two DEFINITIONs are isomorphic
  5865.  */
  5866. static int
  5867. compare_defs (d1, d2)
  5868.      DEFINITION *d1, *d2;
  5869. {
  5870.   register struct reflist *a1, *a2;
  5871.   register U_CHAR *p1 = d1->expansion;
  5872.   register U_CHAR *p2 = d2->expansion;
  5873.   int first = 1;
  5874.  
  5875.   if (d1->nargs != d2->nargs)
  5876.     return 1;
  5877.   if (strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
  5878.     return 1;
  5879.   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
  5880.        a1 = a1->next, a2 = a2->next) {
  5881.     if (!((a1->nchars == a2->nchars && ! bcmp (p1, p2, a1->nchars))
  5882.       || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
  5883.     || a1->argno != a2->argno
  5884.     || a1->stringify != a2->stringify
  5885.     || a1->raw_before != a2->raw_before
  5886.     || a1->raw_after != a2->raw_after)
  5887.       return 1;
  5888.     first = 0;
  5889.     p1 += a1->nchars;
  5890.     p2 += a2->nchars;
  5891.   }
  5892.   if (a1 != a2)
  5893.     return 1;
  5894.   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
  5895.              p2, d2->length - (p2 - d2->expansion), 1))
  5896.     return 1;
  5897.   return 0;
  5898. }
  5899.  
  5900. /* Return 1 if two parts of two macro definitions are effectively different.
  5901.    One of the parts starts at BEG1 and has LEN1 chars;
  5902.    the other has LEN2 chars at BEG2.
  5903.    Any sequence of whitespace matches any other sequence of whitespace.
  5904.    FIRST means these parts are the first of a macro definition;
  5905.     so ignore leading whitespace entirely.
  5906.    LAST means these parts are the last of a macro definition;
  5907.     so ignore trailing whitespace entirely.  */
  5908.  
  5909. static int
  5910. comp_def_part (first, beg1, len1, beg2, len2, last)
  5911.      int first;
  5912.      U_CHAR *beg1, *beg2;
  5913.      int len1, len2;
  5914.      int last;
  5915. {
  5916.   register U_CHAR *end1 = beg1 + len1;
  5917.   register U_CHAR *end2 = beg2 + len2;
  5918.   if (first) {
  5919.     while (beg1 != end1 && is_space[*beg1]) beg1++;
  5920.     while (beg2 != end2 && is_space[*beg2]) beg2++;
  5921.   }
  5922.   if (last) {
  5923.     while (beg1 != end1 && is_space[end1[-1]]) end1--;
  5924.     while (beg2 != end2 && is_space[end2[-1]]) end2--;
  5925.   }
  5926.   while (beg1 != end1 && beg2 != end2) {
  5927.     if (is_space[*beg1] && is_space[*beg2]) {
  5928.       while (beg1 != end1 && is_space[*beg1]) beg1++;
  5929.       while (beg2 != end2 && is_space[*beg2]) beg2++;
  5930.     } else if (*beg1 == *beg2) {
  5931.       beg1++; beg2++;
  5932.     } else break;
  5933.   }
  5934.   return (beg1 != end1) || (beg2 != end2);
  5935. }
  5936.  
  5937. /* Read a replacement list for a macro with parameters.
  5938.    Build the DEFINITION structure.
  5939.    Reads characters of text starting at BUF until END.
  5940.    ARGLIST specifies the formal parameters to look for
  5941.    in the text of the definition; NARGS is the number of args
  5942.    in that list, or -1 for a macro name that wants no argument list.
  5943.    MACRONAME is the macro name itself (so we can avoid recursive expansion)
  5944.    and NAMELEN is its length in characters.
  5945.    
  5946. Note that comments, backslash-newlines, and leading white space
  5947. have already been deleted from the argument.  */
  5948.  
  5949. /* If there is no trailing whitespace, a Newline Space is added at the end
  5950.    to prevent concatenation that would be contrary to the standard.  */
  5951.  
  5952. static DEFINITION *
  5953. collect_expansion (buf, end, nargs, arglist)
  5954.      U_CHAR *buf, *end;
  5955.      int nargs;
  5956.      struct arglist *arglist;
  5957. {
  5958.   DEFINITION *defn;
  5959.   register U_CHAR *p, *limit, *lastp, *exp_p;
  5960.   struct reflist *endpat = NULL;
  5961.   /* Pointer to first nonspace after last ## seen.  */
  5962.   U_CHAR *concat = 0;
  5963.   /* Pointer to first nonspace after last single-# seen.  */
  5964.   U_CHAR *stringify = 0;
  5965.   /* How those tokens were spelled.  */
  5966.   enum sharp_token_type concat_sharp_token_type = NO_SHARP_TOKEN;
  5967.   enum sharp_token_type stringify_sharp_token_type = NO_SHARP_TOKEN;
  5968.   int maxsize;
  5969.   int expected_delimiter = '\0';
  5970.  
  5971.   /* Scan thru the replacement list, ignoring comments and quoted
  5972.      strings, picking up on the macro calls.  It does a linear search
  5973.      thru the arg list on every potential symbol.  Profiling might say
  5974.      that something smarter should happen. */
  5975.  
  5976.   if (end < buf)
  5977.     abort ();
  5978.  
  5979.   /* Find the beginning of the trailing whitespace.  */
  5980.   limit = end;
  5981.   p = buf;
  5982.   while (p < limit && is_space[limit[-1]]) limit--;
  5983.  
  5984.   /* Allocate space for the text in the macro definition.
  5985.      Each input char may or may not need 1 byte,
  5986.      so this is an upper bound.
  5987.      The extra 3 are for invented trailing newline-marker and final null.  */
  5988.   maxsize = (sizeof (DEFINITION)
  5989.          + (limit - p) + 3);
  5990.   defn = (DEFINITION *) xcalloc (1, maxsize);
  5991.  
  5992.   defn->nargs = nargs;
  5993.   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
  5994.   lastp = exp_p;
  5995.  
  5996.   if (p[0] == '#'
  5997.       ? p[1] == '#'
  5998.       : p[0] == '%' && p[1] == ':' && p[2] == '%' && p[3] == ':') {
  5999.     error ("`##' at start of macro definition");
  6000.     p += p[0] == '#' ? 2 : 4;
  6001.   }
  6002.  
  6003.   /* Process the main body of the definition.  */
  6004.   while (p < limit) {
  6005.     int skipped_arg = 0;
  6006.     register U_CHAR c = *p++;
  6007.  
  6008.     *exp_p++ = c;
  6009.  
  6010.     if (!traditional) {
  6011.       switch (c) {
  6012.       case '\'':
  6013.       case '\"':
  6014.         if (expected_delimiter != '\0') {
  6015.           if (c == expected_delimiter)
  6016.             expected_delimiter = '\0';
  6017.         } else
  6018.           expected_delimiter = c;
  6019.     break;
  6020.  
  6021.       case '\\':
  6022.     if (p < limit && expected_delimiter) {
  6023.       /* In a string, backslash goes through
  6024.          and makes next char ordinary.  */
  6025.       *exp_p++ = *p++;
  6026.     }
  6027.     break;
  6028.  
  6029.       case '%':
  6030.     if (!expected_delimiter && *p == ':') {
  6031.       /* %: is not a digraph if preceded by an odd number of '<'s.  */
  6032.       U_CHAR *p0 = p - 1;
  6033.       while (buf < p0 && p0[-1] == '<')
  6034.         p0--;
  6035.       if ((p - p0) & 1) {
  6036.         /* Treat %:%: as ## and %: as #.  */
  6037.         if (p[1] == '%' && p[2] == ':') {
  6038.           p += 2;
  6039.           goto sharp_sharp_token;
  6040.         }
  6041.         if (nargs >= 0) {
  6042.           p++;
  6043.           goto sharp_token;
  6044.         }
  6045.       }
  6046.     }
  6047.     break;
  6048.  
  6049.       case '#':
  6050.     /* # is ordinary inside a string.  */
  6051.     if (expected_delimiter)
  6052.       break;
  6053.     if (*p == '#') {
  6054.     sharp_sharp_token:
  6055.       /* ##: concatenate preceding and following tokens.  */
  6056.       /* Take out the first #, discard preceding whitespace.  */
  6057.       exp_p--;
  6058.       while (exp_p > lastp && is_hor_space[exp_p[-1]])
  6059.         --exp_p;
  6060.       /* Skip the second #.  */
  6061.       p++;
  6062.       concat_sharp_token_type = c;
  6063.       if (is_hor_space[*p]) {
  6064.         concat_sharp_token_type++;
  6065.         p++;
  6066.         SKIP_WHITE_SPACE (p);
  6067.       }
  6068.       concat = p;
  6069.       if (p == limit)
  6070.         error ("`##' at end of macro definition");
  6071.     } else if (nargs >= 0) {
  6072.       /* Single #: stringify following argument ref.
  6073.          Don't leave the # in the expansion.  */
  6074.     sharp_token:
  6075.       exp_p--;
  6076.       stringify_sharp_token_type = c;
  6077.       if (is_hor_space[*p]) {
  6078.         stringify_sharp_token_type++;
  6079.         p++;
  6080.         SKIP_WHITE_SPACE (p);
  6081.       }
  6082.       if (! is_idstart[*p] || nargs == 0)
  6083.         error ("`#' operator is not followed by a macro argument name");
  6084.       else
  6085.         stringify = p;
  6086.     }
  6087.     break;
  6088.       }
  6089.     } else {
  6090.       /* In -traditional mode, recognize arguments inside strings and
  6091.      and character constants, and ignore special properties of #.
  6092.      Arguments inside strings are considered "stringified", but no
  6093.      extra quote marks are supplied.  */
  6094.       switch (c) {
  6095.       case '\'':
  6096.       case '\"':
  6097.     if (expected_delimiter != '\0') {
  6098.       if (c == expected_delimiter)
  6099.         expected_delimiter = '\0';
  6100.     } else
  6101.       expected_delimiter = c;
  6102.     break;
  6103.  
  6104.       case '\\':
  6105.     /* Backslash quotes delimiters and itself, but not macro args.  */
  6106.     if (expected_delimiter != 0 && p < limit
  6107.         && (*p == expected_delimiter || *p == '\\')) {
  6108.       *exp_p++ = *p++;
  6109.       continue;
  6110.     }
  6111.     break;
  6112.  
  6113.       case '/':
  6114.     if (expected_delimiter != '\0') /* No comments inside strings.  */
  6115.       break;
  6116.     if (*p == '*') {
  6117.       /* If we find a comment that wasn't removed by handle_directive,
  6118.          this must be -traditional.  So replace the comment with
  6119.          nothing at all.  */
  6120.       exp_p--;
  6121.       p += 1;
  6122.       while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
  6123.         p++;
  6124. #if 0
  6125.       /* Mark this as a concatenation-point, as if it had been ##.  */
  6126.       concat = p;
  6127. #endif
  6128.     }
  6129.     break;
  6130.       }
  6131.     }
  6132.  
  6133.     /* Handle the start of a symbol.  */
  6134.     if (is_idchar[c] && nargs > 0) {
  6135.       U_CHAR *id_beg = p - 1;
  6136.       int id_len;
  6137.  
  6138.       --exp_p;
  6139.       while (p != limit && is_idchar[*p]) p++;
  6140.       id_len = p - id_beg;
  6141.  
  6142.       if (is_idstart[c]) {
  6143.     register struct arglist *arg;
  6144.  
  6145.     for (arg = arglist; arg != NULL; arg = arg->next) {
  6146.       struct reflist *tpat;
  6147.  
  6148.       if (arg->name[0] == c
  6149.           && arg->length == id_len
  6150.           && bcmp (arg->name, id_beg, id_len) == 0) {
  6151.         enum sharp_token_type tpat_stringify;
  6152.         if (expected_delimiter) {
  6153.           if (warn_stringify) {
  6154.         if (traditional) {
  6155.           warning ("macro argument `%.*s' is stringified.",
  6156.                id_len, arg->name);
  6157.         } else {
  6158.           warning ("macro arg `%.*s' would be stringified with -traditional.",
  6159.                id_len, arg->name);
  6160.         }
  6161.           }
  6162.           /* If ANSI, don't actually substitute inside a string.  */
  6163.           if (!traditional)
  6164.         break;
  6165.           tpat_stringify = SHARP_TOKEN;
  6166.         } else {
  6167.           tpat_stringify
  6168.         = (stringify == id_beg
  6169.            ? stringify_sharp_token_type : NO_SHARP_TOKEN);
  6170.         }
  6171.         /* make a pat node for this arg and append it to the end of
  6172.            the pat list */
  6173.         tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
  6174.         tpat->next = NULL;
  6175.         tpat->raw_before
  6176.           = concat == id_beg ? concat_sharp_token_type : NO_SHARP_TOKEN;
  6177.         tpat->raw_after = NO_SHARP_TOKEN;
  6178.         tpat->rest_args = arg->rest_args;
  6179.         tpat->stringify = tpat_stringify;
  6180.  
  6181.         if (endpat == NULL)
  6182.           defn->pattern = tpat;
  6183.         else
  6184.           endpat->next = tpat;
  6185.         endpat = tpat;
  6186.  
  6187.         tpat->argno = arg->argno;
  6188.         tpat->nchars = exp_p - lastp;
  6189.         {
  6190.           register U_CHAR *p1 = p;
  6191.           SKIP_WHITE_SPACE (p1);
  6192.           if (p1[0]=='#'
  6193.               ? p1[1]=='#'
  6194.           : p1[0]=='%' && p1[1]==':' && p1[2]=='%' && p1[3]==':')
  6195.         tpat->raw_after = p1[0] + (p != p1);
  6196.         }
  6197.         lastp = exp_p;    /* place to start copying from next time */
  6198.         skipped_arg = 1;
  6199.         break;
  6200.       }
  6201.     }
  6202.       }
  6203.  
  6204.       /* If this was not a macro arg, copy it into the expansion.  */
  6205.       if (! skipped_arg) {
  6206.     register U_CHAR *lim1 = p;
  6207.     p = id_beg;
  6208.     while (p != lim1)
  6209.       *exp_p++ = *p++;
  6210.     if (stringify == id_beg)
  6211.       error ("`#' operator should be followed by a macro argument name");
  6212.       }
  6213.     }
  6214.   }
  6215.  
  6216.   if (!traditional && expected_delimiter == 0) {
  6217.     /* If ANSI, put in a newline-space marker to prevent token pasting.
  6218.        But not if "inside a string" (which in ANSI mode happens only for
  6219.        -D option).  */
  6220.     *exp_p++ = '\n';
  6221.     *exp_p++ = ' ';
  6222.   }
  6223.  
  6224.   *exp_p = '\0';
  6225.  
  6226.   defn->length = exp_p - defn->expansion;
  6227.  
  6228.   /* Crash now if we overrun the allocated size.  */
  6229.   if (defn->length + 1 > maxsize)
  6230.     abort ();
  6231.  
  6232. #if 0
  6233. /* This isn't worth the time it takes.  */
  6234.   /* give back excess storage */
  6235.   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
  6236. #endif
  6237.  
  6238.   return defn;
  6239. }
  6240.  
  6241. static int
  6242. do_assert (buf, limit, op, keyword)
  6243.      U_CHAR *buf, *limit;
  6244.      FILE_BUF *op;
  6245.      struct directive *keyword;
  6246. {
  6247.   U_CHAR *bp;            /* temp ptr into input buffer */
  6248.   U_CHAR *symname;        /* remember where symbol name starts */
  6249.   int sym_length;        /* and how long it is */
  6250.   struct arglist *tokens = NULL;
  6251.  
  6252.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6253.     pedwarn ("ANSI C does not allow `#assert'");
  6254.  
  6255.   bp = buf;
  6256.  
  6257.   while (is_hor_space[*bp])
  6258.     bp++;
  6259.  
  6260.   symname = bp;            /* remember where it starts */
  6261.   sym_length = check_macro_name (bp, "assertion");
  6262.   bp += sym_length;
  6263.   /* #define doesn't do this, but we should.  */
  6264.   SKIP_WHITE_SPACE (bp);
  6265.  
  6266.   /* Lossage will occur if identifiers or control tokens are broken
  6267.      across lines using backslash.  This is not the right place to take
  6268.      care of that. */
  6269.  
  6270.   if (*bp != '(') {
  6271.     error ("missing token-sequence in `#assert'");
  6272.     return 1;
  6273.   }
  6274.  
  6275.   {
  6276.     int error_flag = 0;
  6277.  
  6278.     bp++;            /* skip '(' */
  6279.     SKIP_WHITE_SPACE (bp);
  6280.  
  6281.     tokens = read_token_list (&bp, limit, &error_flag);
  6282.     if (error_flag)
  6283.       return 1;
  6284.     if (tokens == 0) {
  6285.       error ("empty token-sequence in `#assert'");
  6286.       return 1;
  6287.     }
  6288.  
  6289.     ++bp;            /* skip paren */
  6290.     SKIP_WHITE_SPACE (bp);
  6291.   }
  6292.  
  6293.   /* If this name isn't already an assertion name, make it one.
  6294.      Error if it was already in use in some other way.  */
  6295.  
  6296.   {
  6297.     ASSERTION_HASHNODE *hp;
  6298.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6299.     struct tokenlist_list *value
  6300.       = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
  6301.  
  6302.     hp = assertion_lookup (symname, sym_length, hashcode);
  6303.     if (hp == NULL) {
  6304.       if (sym_length == 7 && ! bcmp (symname, "defined", 7))
  6305.     error ("`defined' redefined as assertion");
  6306.       hp = assertion_install (symname, sym_length, hashcode);
  6307.     }
  6308.  
  6309.     /* Add the spec'd token-sequence to the list of such.  */
  6310.     value->tokens = tokens;
  6311.     value->next = hp->value;
  6312.     hp->value = value;
  6313.   }
  6314.  
  6315.   return 0;
  6316. }
  6317.  
  6318. static int
  6319. do_unassert (buf, limit, op, keyword)
  6320.      U_CHAR *buf, *limit;
  6321.      FILE_BUF *op;
  6322.      struct directive *keyword;
  6323. {
  6324.   U_CHAR *bp;            /* temp ptr into input buffer */
  6325.   U_CHAR *symname;        /* remember where symbol name starts */
  6326.   int sym_length;        /* and how long it is */
  6327.  
  6328.   struct arglist *tokens = NULL;
  6329.   int tokens_specified = 0;
  6330.  
  6331.   if (pedantic && done_initializing && !instack[indepth].system_header_p)
  6332.     pedwarn ("ANSI C does not allow `#unassert'");
  6333.  
  6334.   bp = buf;
  6335.  
  6336.   while (is_hor_space[*bp])
  6337.     bp++;
  6338.  
  6339.   symname = bp;            /* remember where it starts */
  6340.   sym_length = check_macro_name (bp, "assertion");
  6341.   bp += sym_length;
  6342.   /* #define doesn't do this, but we should.  */
  6343.   SKIP_WHITE_SPACE (bp);
  6344.  
  6345.   /* Lossage will occur if identifiers or control tokens are broken
  6346.      across lines using backslash.  This is not the right place to take
  6347.      care of that. */
  6348.  
  6349.   if (*bp == '(') {
  6350.     int error_flag = 0;
  6351.  
  6352.     bp++;            /* skip '(' */
  6353.     SKIP_WHITE_SPACE (bp);
  6354.  
  6355.     tokens = read_token_list (&bp, limit, &error_flag);
  6356.     if (error_flag)
  6357.       return 1;
  6358.     if (tokens == 0) {
  6359.       error ("empty token list in `#unassert'");
  6360.       return 1;
  6361.     }
  6362.  
  6363.     tokens_specified = 1;
  6364.  
  6365.     ++bp;            /* skip paren */
  6366.     SKIP_WHITE_SPACE (bp);
  6367.   }
  6368.  
  6369.   {
  6370.     ASSERTION_HASHNODE *hp;
  6371.     int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
  6372.     struct tokenlist_list *tail, *prev;
  6373.  
  6374.     hp = assertion_lookup (symname, sym_length, hashcode);
  6375.     if (hp == NULL)
  6376.       return 1;
  6377.  
  6378.     /* If no token list was specified, then eliminate this assertion
  6379.        entirely.  */
  6380.     if (! tokens_specified) {
  6381.       struct tokenlist_list *next;
  6382.       for (tail = hp->value; tail; tail = next) {
  6383.     next = tail->next;
  6384.     free_token_list (tail->tokens);
  6385.     free (tail);
  6386.       }
  6387.       delete_assertion (hp);
  6388.     } else {
  6389.       /* If a list of tokens was given, then delete any matching list.  */
  6390.  
  6391.       tail = hp->value;
  6392.       prev = 0;
  6393.       while (tail) {
  6394.     struct tokenlist_list *next = tail->next;
  6395.     if (compare_token_lists (tail->tokens, tokens)) {
  6396.       if (prev)
  6397.         prev->next = next;
  6398.       else
  6399.         hp->value = tail->next;
  6400.       free_token_list (tail->tokens);
  6401.       free (tail);
  6402.     } else {
  6403.       prev = tail;
  6404.     }
  6405.     tail = next;
  6406.       }
  6407.     }
  6408.   }
  6409.  
  6410.   return 0;
  6411. }
  6412.  
  6413. /* Test whether there is an assertion named NAME
  6414.    and optionally whether it has an asserted token list TOKENS.
  6415.    NAME is not null terminated; its length is SYM_LENGTH.
  6416.    If TOKENS_SPECIFIED is 0, then don't check for any token list.  */
  6417.  
  6418. int
  6419. check_assertion (name, sym_length, tokens_specified, tokens)
  6420.      U_CHAR *name;
  6421.      int sym_length;
  6422.      int tokens_specified;
  6423.      struct arglist *tokens;
  6424. {
  6425.   ASSERTION_HASHNODE *hp;
  6426.   int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
  6427.  
  6428.   if (pedantic && !instack[indepth].system_header_p)
  6429.     pedwarn ("ANSI C does not allow testing assertions");
  6430.  
  6431.   hp = assertion_lookup (name, sym_length, hashcode);
  6432.   if (hp == NULL)
  6433.     /* It is not an assertion; just return false.  */
  6434.     return 0;
  6435.  
  6436.   /* If no token list was specified, then value is 1.  */
  6437.   if (! tokens_specified)
  6438.     return 1;
  6439.  
  6440.   {
  6441.     struct tokenlist_list *tail;
  6442.  
  6443.     tail = hp->value;
  6444.  
  6445.     /* If a list of tokens was given,
  6446.        then succeed if the assertion records a matching list.  */
  6447.  
  6448.     while (tail) {
  6449.       if (compare_token_lists (tail->tokens, tokens))
  6450.     return 1;
  6451.       tail = tail->next;
  6452.     }
  6453.  
  6454.     /* Fail if the assertion has no matching list.  */
  6455.     return 0;
  6456.   }
  6457. }
  6458.  
  6459. /* Compare two lists of tokens for equality including order of tokens.  */
  6460.  
  6461. static int
  6462. compare_token_lists (l1, l2)
  6463.      struct arglist *l1, *l2;
  6464. {
  6465.   while (l1 && l2) {
  6466.     if (l1->length != l2->length)
  6467.       return 0;
  6468.     if (bcmp (l1->name, l2->name, l1->length))
  6469.       return 0;
  6470.     l1 = l1->next;
  6471.     l2 = l2->next;
  6472.   }
  6473.  
  6474.   /* Succeed if both lists end at the same time.  */
  6475.   return l1 == l2;
  6476. }
  6477.  
  6478. /* Read a space-separated list of tokens ending in a close parenthesis.
  6479.    Return a list of strings, in the order they were written.
  6480.    (In case of error, return 0 and store -1 in *ERROR_FLAG.)
  6481.    Parse the text starting at *BPP, and update *BPP.
  6482.    Don't parse beyond LIMIT.  */
  6483.  
  6484. static struct arglist *
  6485. read_token_list (bpp, limit, error_flag)
  6486.      U_CHAR **bpp;
  6487.      U_CHAR *limit;
  6488.      int *error_flag;
  6489. {
  6490.   struct arglist *token_ptrs = 0;
  6491.   U_CHAR *bp = *bpp;
  6492.   int depth = 1;
  6493.  
  6494.   *error_flag = 0;
  6495.  
  6496.   /* Loop over the assertion value tokens.  */
  6497.   while (depth > 0) {
  6498.     struct arglist *temp;
  6499.     int eofp = 0;
  6500.     U_CHAR *beg = bp;
  6501.  
  6502.     /* Find the end of the token.  */
  6503.     if (*bp == '(') {
  6504.       bp++;
  6505.       depth++;
  6506.     } else if (*bp == ')') {
  6507.       depth--;
  6508.       if (depth == 0)
  6509.     break;
  6510.       bp++;
  6511.     } else if (*bp == '"' || *bp == '\'')
  6512.       bp = skip_quoted_string (bp, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  6513.     else
  6514.       while (! is_hor_space[*bp] && *bp != '(' && *bp != ')'
  6515.          && *bp != '"' && *bp != '\'' && bp != limit)
  6516.     bp++;
  6517.  
  6518.     temp = (struct arglist *) xmalloc (sizeof (struct arglist));
  6519.     temp->name = (U_CHAR *) xmalloc (bp - beg + 1);
  6520.     bcopy ((char *) beg, (char *) temp->name, bp - beg);
  6521.     temp->name[bp - beg] = 0;
  6522.     temp->next = token_ptrs;
  6523.     token_ptrs = temp;
  6524.     temp->length = bp - beg;
  6525.  
  6526.     SKIP_WHITE_SPACE (bp);
  6527.  
  6528.     if (bp >= limit) {
  6529.       error ("unterminated token sequence in `#assert' or `#unassert'");
  6530.       *error_flag = -1;
  6531.       return 0;
  6532.     }
  6533.   }
  6534.   *bpp = bp;
  6535.  
  6536.   /* We accumulated the names in reverse order.
  6537.      Now reverse them to get the proper order.  */
  6538.   {
  6539.     register struct arglist *prev = 0, *this, *next;
  6540.     for (this = token_ptrs; this; this = next) {
  6541.       next = this->next;
  6542.       this->next = prev;
  6543.       prev = this;
  6544.     }
  6545.     return prev;
  6546.   }
  6547. }
  6548.  
  6549. static void
  6550. free_token_list (tokens)
  6551.      struct arglist *tokens;
  6552. {
  6553.   while (tokens) {
  6554.     struct arglist *next = tokens->next;
  6555.     free (tokens->name);
  6556.     free (tokens);
  6557.     tokens = next;
  6558.   }
  6559. }
  6560.  
  6561. /*
  6562.  * Install a name in the assertion hash table.
  6563.  *
  6564.  * If LEN is >= 0, it is the length of the name.
  6565.  * Otherwise, compute the length by scanning the entire name.
  6566.  *
  6567.  * If HASH is >= 0, it is the precomputed hash code.
  6568.  * Otherwise, compute the hash code.
  6569.  */
  6570. static ASSERTION_HASHNODE *
  6571. assertion_install (name, len, hash)
  6572.      U_CHAR *name;
  6573.      int len;
  6574.      int hash;
  6575. {
  6576.   register ASSERTION_HASHNODE *hp;
  6577.   register int i, bucket;
  6578.   register U_CHAR *p, *q;
  6579.  
  6580.   i = sizeof (ASSERTION_HASHNODE) + len + 1;
  6581.   hp = (ASSERTION_HASHNODE *) xmalloc (i);
  6582.   bucket = hash;
  6583.   hp->bucket_hdr = &assertion_hashtab[bucket];
  6584.   hp->next = assertion_hashtab[bucket];
  6585.   assertion_hashtab[bucket] = hp;
  6586.   hp->prev = NULL;
  6587.   if (hp->next != NULL)
  6588.     hp->next->prev = hp;
  6589.   hp->length = len;
  6590.   hp->value = 0;
  6591.   hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
  6592.   p = hp->name;
  6593.   q = name;
  6594.   for (i = 0; i < len; i++)
  6595.     *p++ = *q++;
  6596.   hp->name[len] = 0;
  6597.   return hp;
  6598. }
  6599.  
  6600. /*
  6601.  * find the most recent hash node for name name (ending with first
  6602.  * non-identifier char) installed by install
  6603.  *
  6604.  * If LEN is >= 0, it is the length of the name.
  6605.  * Otherwise, compute the length by scanning the entire name.
  6606.  *
  6607.  * If HASH is >= 0, it is the precomputed hash code.
  6608.  * Otherwise, compute the hash code.
  6609.  */
  6610. static ASSERTION_HASHNODE *
  6611. assertion_lookup (name, len, hash)
  6612.      U_CHAR *name;
  6613.      int len;
  6614.      int hash;
  6615. {
  6616.   register ASSERTION_HASHNODE *bucket;
  6617.  
  6618.   bucket = assertion_hashtab[hash];
  6619.   while (bucket) {
  6620.     if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
  6621.       return bucket;
  6622.     bucket = bucket->next;
  6623.   }
  6624.   return NULL;
  6625. }
  6626.  
  6627. static void
  6628. delete_assertion (hp)
  6629.      ASSERTION_HASHNODE *hp;
  6630. {
  6631.  
  6632.   if (hp->prev != NULL)
  6633.     hp->prev->next = hp->next;
  6634.   if (hp->next != NULL)
  6635.     hp->next->prev = hp->prev;
  6636.  
  6637.   /* make sure that the bucket chain header that
  6638.      the deleted guy was on points to the right thing afterwards. */
  6639.   if (hp == *hp->bucket_hdr)
  6640.     *hp->bucket_hdr = hp->next;
  6641.  
  6642.   free (hp);
  6643. }
  6644.  
  6645. /*
  6646.  * interpret #line directive.  Remembers previously seen fnames
  6647.  * in its very own hash table.
  6648.  */
  6649. #define FNAME_HASHSIZE 37
  6650.  
  6651. static int
  6652. do_line (buf, limit, op, keyword)
  6653.      U_CHAR *buf, *limit;
  6654.      FILE_BUF *op;
  6655.      struct directive *keyword;
  6656. {
  6657.   register U_CHAR *bp;
  6658.   FILE_BUF *ip = &instack[indepth];
  6659.   FILE_BUF tem;
  6660.   int new_lineno;
  6661.   enum file_change_code file_change = same_file;
  6662.  
  6663.   /* Expand any macros.  */
  6664.   tem = expand_to_temp_buffer (buf, limit, 0, 0);
  6665.  
  6666.   /* Point to macroexpanded line, which is null-terminated now.  */
  6667.   bp = tem.buf;
  6668.   SKIP_WHITE_SPACE (bp);
  6669.  
  6670.   if (!isdigit (*bp)) {
  6671.     error ("invalid format `#line' directive");
  6672.     return 0;
  6673.   }
  6674.  
  6675.   /* The Newline at the end of this line remains to be processed.
  6676.      To put the next line at the specified line number,
  6677.      we must store a line number now that is one less.  */
  6678.   new_lineno = atoi ((char *) bp) - 1;
  6679.  
  6680.   /* NEW_LINENO is one less than the actual line number here.  */
  6681.   if (pedantic && new_lineno < 0)
  6682.     pedwarn ("line number out of range in `#line' directive");
  6683.  
  6684.   /* skip over the line number.  */
  6685.   while (isdigit (*bp))
  6686.     bp++;
  6687.  
  6688. #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
  6689.   if (*bp && !is_space[*bp]) {
  6690.     error ("invalid format `#line' directive");
  6691.     return;
  6692.   }
  6693. #endif
  6694.  
  6695.   SKIP_WHITE_SPACE (bp);
  6696.  
  6697.   if (*bp == '\"') {
  6698.     static HASHNODE *fname_table[FNAME_HASHSIZE];
  6699.     HASHNODE *hp, **hash_bucket;
  6700.     U_CHAR *fname, *p;
  6701.     int fname_length;
  6702.  
  6703.     fname = ++bp;
  6704.  
  6705.     /* Turn the file name, which is a character string literal,
  6706.        into a null-terminated string.  Do this in place.  */
  6707.     p = bp;
  6708.     for (;;)
  6709.       switch ((*p++ = *bp++)) {
  6710.       case '\0':
  6711.     error ("invalid format `#line' directive");
  6712.     return 0;
  6713.  
  6714.       case '\\':
  6715.     {
  6716.       char *bpc = (char *) bp;
  6717.       int c = parse_escape (&bpc);
  6718.       bp = (U_CHAR *) bpc;
  6719.       if (c < 0)
  6720.         p--;
  6721.       else
  6722.         p[-1] = c;
  6723.     }
  6724.     break;
  6725.  
  6726.       case '\"':
  6727.     p[-1] = 0;
  6728.     goto fname_done;
  6729.       }
  6730.   fname_done:
  6731.     fname_length = p - fname;
  6732.  
  6733.     SKIP_WHITE_SPACE (bp);
  6734.     if (*bp) {
  6735.       if (pedantic)
  6736.     pedwarn ("garbage at end of `#line' directive");
  6737.       if (*bp == '1')
  6738.     file_change = enter_file;
  6739.       else if (*bp == '2')
  6740.     file_change = leave_file;
  6741.       else if (*bp == '3')
  6742.     ip->system_header_p = 1;
  6743.       else if (*bp == '4')
  6744.     ip->system_header_p = 2;
  6745.       else {
  6746.     error ("invalid format `#line' directive");
  6747.     return 0;
  6748.       }
  6749.  
  6750.       bp++;
  6751.       SKIP_WHITE_SPACE (bp);
  6752.       if (*bp == '3') {
  6753.     ip->system_header_p = 1;
  6754.     bp++;
  6755.     SKIP_WHITE_SPACE (bp);
  6756.       }
  6757.       if (*bp == '4') {
  6758.     ip->system_header_p = 2;
  6759.     bp++;
  6760.     SKIP_WHITE_SPACE (bp);
  6761.       }
  6762.       if (*bp) {
  6763.     error ("invalid format `#line' directive");
  6764.     return 0;
  6765.       }
  6766.     }
  6767.  
  6768.     hash_bucket =
  6769.       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
  6770.     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
  6771.       if (hp->length == fname_length &&
  6772.       bcmp (hp->value.cpval, fname, fname_length) == 0) {
  6773.     ip->nominal_fname = hp->value.cpval;
  6774.     break;
  6775.       }
  6776.     if (hp == 0) {
  6777.       /* Didn't find it; cons up a new one.  */
  6778.       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
  6779.       hp->next = *hash_bucket;
  6780.       *hash_bucket = hp;
  6781.  
  6782.       hp->length = fname_length;
  6783.       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
  6784.       bcopy (fname, hp->value.cpval, fname_length);
  6785.     }
  6786.   } else if (*bp) {
  6787.     error ("invalid format `#line' directive");
  6788.     return 0;
  6789.   }
  6790.  
  6791.   ip->lineno = new_lineno;
  6792.   output_line_directive (ip, op, 0, file_change);
  6793.   check_expand (op, ip->length - (ip->bufp - ip->buf));
  6794.   return 0;
  6795. }
  6796.  
  6797. /*
  6798.  * remove the definition of a symbol from the symbol table.
  6799.  * according to un*x /lib/cpp, it is not an error to undef
  6800.  * something that has no definitions, so it isn't one here either.
  6801.  */
  6802.  
  6803. static int
  6804. do_undef (buf, limit, op, keyword)
  6805.      U_CHAR *buf, *limit;
  6806.      FILE_BUF *op;
  6807.      struct directive *keyword;
  6808. {
  6809.   int sym_length;
  6810.   HASHNODE *hp;
  6811.   U_CHAR *orig_buf = buf;
  6812.  
  6813.   /* If this is a precompiler run (with -pcp) pass thru #undef directives.  */
  6814.   if (pcp_outfile && op)
  6815.     pass_thru_directive (buf, limit, op, keyword);
  6816.  
  6817.   SKIP_WHITE_SPACE (buf);
  6818.   sym_length = check_macro_name (buf, "macro");
  6819.  
  6820.   while ((hp = lookup (buf, sym_length, -1)) != NULL) {
  6821.     /* If we are generating additional info for debugging (with -g) we
  6822.        need to pass through all effective #undef directives.  */
  6823.     if (debug_output && op)
  6824.       pass_thru_directive (orig_buf, limit, op, keyword);
  6825.     if (hp->type != T_MACRO)
  6826.       warning ("undefining `%s'", hp->name);
  6827.     delete_macro (hp);
  6828.   }
  6829.  
  6830.   if (pedantic) {
  6831.     buf += sym_length;
  6832.     SKIP_WHITE_SPACE (buf);
  6833.     if (buf != limit)
  6834.       pedwarn ("garbage after `#undef' directive");
  6835.   }
  6836.   return 0;
  6837. }
  6838.  
  6839. /*
  6840.  * Report an error detected by the program we are processing.
  6841.  * Use the text of the line in the error message.
  6842.  * (We use error because it prints the filename & line#.)
  6843.  */
  6844.  
  6845. static int
  6846. do_error (buf, limit, op, keyword)
  6847.      U_CHAR *buf, *limit;
  6848.      FILE_BUF *op;
  6849.      struct directive *keyword;
  6850. {
  6851.   int length = limit - buf;
  6852.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6853.   bcopy ((char *) buf, (char *) copy, length);
  6854.   copy[length] = 0;
  6855.   SKIP_WHITE_SPACE (copy);
  6856.   error ("#error %s", copy);
  6857.   return 0;
  6858. }
  6859.  
  6860. /*
  6861.  * Report a warning detected by the program we are processing.
  6862.  * Use the text of the line in the warning message, then continue.
  6863.  * (We use error because it prints the filename & line#.)
  6864.  */
  6865.  
  6866. static int
  6867. do_warning (buf, limit, op, keyword)
  6868.      U_CHAR *buf, *limit;
  6869.      FILE_BUF *op;
  6870.      struct directive *keyword;
  6871. {
  6872.   int length = limit - buf;
  6873.   U_CHAR *copy = (U_CHAR *) xmalloc (length + 1);
  6874.   bcopy ((char *) buf, (char *) copy, length);
  6875.   copy[length] = 0;
  6876.   SKIP_WHITE_SPACE (copy);
  6877.   warning ("#warning %s", copy);
  6878.   return 0;
  6879. }
  6880.  
  6881. /* Remember the name of the current file being read from so that we can
  6882.    avoid ever including it again.  */
  6883.  
  6884. static void
  6885. do_once ()
  6886. {
  6887.   int i;
  6888.   FILE_BUF *ip = NULL;
  6889.  
  6890.   for (i = indepth; i >= 0; i--)
  6891.     if (instack[i].fname != NULL) {
  6892.       ip = &instack[i];
  6893.       break;
  6894.     }
  6895.  
  6896.   if (ip != NULL) {
  6897.     struct file_name_list *new;
  6898.     
  6899.     new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
  6900.     new->next = dont_repeat_files;
  6901.     dont_repeat_files = new;
  6902.     new->fname = savestring (ip->fname);
  6903.     new->control_macro = 0;
  6904.     new->got_name_map = 0;
  6905.     new->c_system_include_path = 0;
  6906.   }
  6907. }
  6908.  
  6909. /* #ident has already been copied to the output file, so just ignore it.  */
  6910.  
  6911. static int
  6912. do_ident (buf, limit, op, keyword)
  6913.      U_CHAR *buf, *limit;
  6914.      FILE_BUF *op;
  6915.      struct directive *keyword;
  6916. {
  6917.   FILE_BUF trybuf;
  6918.   int len;
  6919.  
  6920.   /* Allow #ident in system headers, since that's not user's fault.  */
  6921.   if (pedantic && !instack[indepth].system_header_p)
  6922.     pedwarn ("ANSI C does not allow `#ident'");
  6923.  
  6924.   trybuf = expand_to_temp_buffer (buf, limit, 0, 0);
  6925.   buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
  6926.   bcopy ((char *) trybuf.buf, (char *) buf, trybuf.bufp - trybuf.buf);
  6927.   limit = buf + (trybuf.bufp - trybuf.buf);
  6928.   len = (limit - buf);
  6929.   free (trybuf.buf);
  6930.  
  6931.   /* Output directive name.  */
  6932.   check_expand (op, 7);
  6933.   bcopy ("#ident ", (char *) op->bufp, 7);
  6934.   op->bufp += 7;
  6935.  
  6936.   /* Output the expanded argument line.  */
  6937.   check_expand (op, len);
  6938.   bcopy ((char *) buf, (char *) op->bufp, len);
  6939.   op->bufp += len;
  6940.  
  6941.   return 0;
  6942. }
  6943.  
  6944. /* #pragma and its argument line have already been copied to the output file.
  6945.    Just check for some recognized pragmas that need validation here.  */
  6946.  
  6947. static int
  6948. do_pragma (buf, limit, op, keyword)
  6949.      U_CHAR *buf, *limit;
  6950.      FILE_BUF *op;
  6951.      struct directive *keyword;
  6952. {
  6953.   SKIP_WHITE_SPACE (buf);
  6954.   if (!strncmp ((char *) buf, "once", 4)) {
  6955.     /* Allow #pragma once in system headers, since that's not the user's
  6956.        fault.  */
  6957.     if (!instack[indepth].system_header_p)
  6958.       warning ("`#pragma once' is obsolete");
  6959.     do_once ();
  6960.   }
  6961.  
  6962.   if (!strncmp ((char *) buf, "implementation", 14)) {
  6963.     /* Be quiet about `#pragma implementation' for a file only if it hasn't
  6964.        been included yet.  */
  6965.     struct file_name_list *ptr;
  6966.     U_CHAR *p = buf + 14, *fname, *inc_fname;
  6967.     SKIP_WHITE_SPACE (p);
  6968.     if (*p == '\n' || *p != '\"')
  6969.       return 0;
  6970.  
  6971.     fname = p + 1;
  6972.     if ((p = (U_CHAR *) index ((char *) fname, '\"')))
  6973.       *p = '\0';
  6974.     
  6975.     for (ptr = all_include_files; ptr; ptr = ptr->next) {
  6976.       inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
  6977.       inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
  6978.       if (inc_fname && !strcmp ((char *) inc_fname, (char *) fname))
  6979.     warning ("`#pragma implementation' for `%s' appears after file is included",
  6980.          fname);
  6981.     }
  6982.   }
  6983.  
  6984.   return 0;
  6985. }
  6986.  
  6987. #if 0
  6988. /* This was a fun hack, but #pragma seems to start to be useful.
  6989.    By failing to recognize it, we pass it through unchanged to cc1.  */
  6990.  
  6991. /*
  6992.  * the behavior of the #pragma directive is implementation defined.
  6993.  * this implementation defines it as follows.
  6994.  */
  6995.  
  6996. static int
  6997. do_pragma ()
  6998. {
  6999.   close (0);
  7000.   if (open ("/dev/tty", O_RDONLY, 0666) != 0)
  7001.     goto nope;
  7002.   close (1);
  7003.   if (open ("/dev/tty", O_WRONLY, 0666) != 1)
  7004.     goto nope;
  7005.   execl ("/usr/games/hack", "#pragma", 0);
  7006.   execl ("/usr/games/rogue", "#pragma", 0);
  7007.   execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
  7008.   execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
  7009. nope:
  7010.   fatal ("You are in a maze of twisty compiler features, all different");
  7011. }
  7012. #endif
  7013.  
  7014. #ifdef SCCS_DIRECTIVE
  7015.  
  7016. /* Just ignore #sccs, on systems where we define it at all.  */
  7017.  
  7018. static int
  7019. do_sccs (buf, limit, op, keyword)
  7020.      U_CHAR *buf, *limit;
  7021.      FILE_BUF *op;
  7022.      struct directive *keyword;
  7023. {
  7024.   if (pedantic)
  7025.     pedwarn ("ANSI C does not allow `#sccs'");
  7026.   return 0;
  7027. }
  7028.  
  7029. #endif /* defined (SCCS_DIRECTIVE) */
  7030.  
  7031. /*
  7032.  * handle #if directive by
  7033.  *   1) inserting special `defined' keyword into the hash table
  7034.  *    that gets turned into 0 or 1 by special_symbol (thus,
  7035.  *    if the luser has a symbol called `defined' already, it won't
  7036.  *      work inside the #if directive)
  7037.  *   2) rescan the input into a temporary output buffer
  7038.  *   3) pass the output buffer to the yacc parser and collect a value
  7039.  *   4) clean up the mess left from steps 1 and 2.
  7040.  *   5) call conditional_skip to skip til the next #endif (etc.),
  7041.  *      or not, depending on the value from step 3.
  7042.  */
  7043.  
  7044. static int
  7045. do_if (buf, limit, op, keyword)
  7046.      U_CHAR *buf, *limit;
  7047.      FILE_BUF *op;
  7048.      struct directive *keyword;
  7049. {
  7050.   HOST_WIDE_INT value;
  7051.   FILE_BUF *ip = &instack[indepth];
  7052.  
  7053.   value = eval_if_expression (buf, limit - buf);
  7054.   conditional_skip (ip, value == 0, T_IF, NULL_PTR, op);
  7055.   return 0;
  7056. }
  7057.  
  7058. /*
  7059.  * handle a #elif directive by not changing  if_stack  either.
  7060.  * see the comment above do_else.
  7061.  */
  7062.  
  7063. static int
  7064. do_elif (buf, limit, op, keyword)
  7065.      U_CHAR *buf, *limit;
  7066.      FILE_BUF *op;
  7067.      struct directive *keyword;
  7068. {
  7069.   HOST_WIDE_INT value;
  7070.   FILE_BUF *ip = &instack[indepth];
  7071.  
  7072.   if (if_stack == instack[indepth].if_stack) {
  7073.     error ("`#elif' not within a conditional");
  7074.     return 0;
  7075.   } else {
  7076.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7077.       error ("`#elif' after `#else'");
  7078.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7079.       if (if_stack->fname != NULL && ip->fname != NULL &&
  7080.       strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7081.     fprintf (stderr, ", file %s", if_stack->fname);
  7082.       fprintf (stderr, ")\n");
  7083.     }
  7084.     if_stack->type = T_ELIF;
  7085.   }
  7086.  
  7087.   if (if_stack->if_succeeded)
  7088.     skip_if_group (ip, 0, op);
  7089.   else {
  7090.     value = eval_if_expression (buf, limit - buf);
  7091.     if (value == 0)
  7092.       skip_if_group (ip, 0, op);
  7093.     else {
  7094.       ++if_stack->if_succeeded;    /* continue processing input */
  7095.       output_line_directive (ip, op, 1, same_file);
  7096.     }
  7097.   }
  7098.   return 0;
  7099. }
  7100.  
  7101. /*
  7102.  * evaluate a #if expression in BUF, of length LENGTH,
  7103.  * then parse the result as a C expression and return the value as an int.
  7104.  */
  7105. static HOST_WIDE_INT
  7106. eval_if_expression (buf, length)
  7107.      U_CHAR *buf;
  7108.      int length;
  7109. {
  7110.   FILE_BUF temp_obuf;
  7111.   HASHNODE *save_defined;
  7112.   HOST_WIDE_INT value;
  7113.  
  7114.   save_defined = install ((U_CHAR *) "defined", -1, T_SPEC_DEFINED,
  7115.               NULL_PTR, -1);
  7116.   pcp_inside_if = 1;
  7117.   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0, 1);
  7118.   pcp_inside_if = 0;
  7119.   delete_macro (save_defined);    /* clean up special symbol */
  7120.  
  7121.   value = parse_c_expression ((char *) temp_obuf.buf);
  7122.  
  7123.   free (temp_obuf.buf);
  7124.  
  7125.   return value;
  7126. }
  7127.  
  7128. /*
  7129.  * routine to handle ifdef/ifndef.  Try to look up the symbol,
  7130.  * then do or don't skip to the #endif/#else/#elif depending
  7131.  * on what directive is actually being processed.
  7132.  */
  7133.  
  7134. static int
  7135. do_xifdef (buf, limit, op, keyword)
  7136.      U_CHAR *buf, *limit;
  7137.      FILE_BUF *op;
  7138.      struct directive *keyword;
  7139. {
  7140.   int skip;
  7141.   FILE_BUF *ip = &instack[indepth];
  7142.   U_CHAR *end; 
  7143.   int start_of_file = 0;
  7144.   U_CHAR *control_macro = 0;
  7145.  
  7146.   /* Detect a #ifndef at start of file (not counting comments).  */
  7147.   if (ip->fname != 0 && keyword->type == T_IFNDEF) {
  7148.     U_CHAR *p = ip->buf;
  7149.     while (p != directive_start) {
  7150.       U_CHAR c = *p++;
  7151.       if (is_space[c])
  7152.     ;
  7153.       /* Make no special provision for backslash-newline here; this is
  7154.      slower if backslash-newlines are present, but it's correct,
  7155.      and it's not worth it to tune for the rare backslash-newline.  */
  7156.       else if (c == '/'
  7157.            && (*p == '*' || (cplusplus_comments && *p == '/'))) {
  7158.     /* Skip this comment.  */
  7159.     int junk = 0;
  7160.     U_CHAR *save_bufp = ip->bufp;
  7161.     ip->bufp = p + 1;
  7162.     p = skip_to_end_of_comment (ip, &junk, 1);
  7163.     ip->bufp = save_bufp;
  7164.       } else {
  7165.     goto fail;
  7166.       }
  7167.     }
  7168.     /* If we get here, this conditional is the beginning of the file.  */
  7169.     start_of_file = 1;
  7170.   fail: ;
  7171.   }
  7172.  
  7173.   /* Discard leading and trailing whitespace.  */
  7174.   SKIP_WHITE_SPACE (buf);
  7175.   while (limit != buf && is_hor_space[limit[-1]]) limit--;
  7176.  
  7177.   /* Find the end of the identifier at the beginning.  */
  7178.   for (end = buf; is_idchar[*end]; end++);
  7179.  
  7180.   if (end == buf) {
  7181.     skip = (keyword->type == T_IFDEF);
  7182.     if (! traditional)
  7183.       pedwarn (end == limit ? "`#%s' with no argument"
  7184.            : "`#%s' argument starts with punctuation",
  7185.            keyword->name);
  7186.   } else {
  7187.     HASHNODE *hp;
  7188.  
  7189.     if (pedantic && buf[0] >= '0' && buf[0] <= '9')
  7190.       pedwarn ("`#%s' argument starts with a digit", keyword->name);
  7191.     else if (end != limit && !traditional)
  7192.       pedwarn ("garbage at end of `#%s' argument", keyword->name);
  7193.  
  7194.     hp = lookup (buf, end-buf, -1);
  7195.  
  7196.     if (pcp_outfile) {
  7197.       /* Output a precondition for this macro.  */
  7198.       if (hp &&
  7199.       (hp->type == T_CONST
  7200.        || (hp->type == T_MACRO && hp->value.defn->predefined)))
  7201.     fprintf (pcp_outfile, "#define %s\n", hp->name);
  7202.       else {
  7203.     U_CHAR *cp = buf;
  7204.     fprintf (pcp_outfile, "#undef ");
  7205.     while (is_idchar[*cp]) /* Ick! */
  7206.       fputc (*cp++, pcp_outfile);
  7207.     putc ('\n', pcp_outfile);
  7208.       }
  7209.     }
  7210.  
  7211.     skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
  7212.     if (start_of_file && !skip) {
  7213.       control_macro = (U_CHAR *) xmalloc (end - buf + 1);
  7214.       bcopy ((char *) buf, (char *) control_macro, end - buf);
  7215.       control_macro[end - buf] = 0;
  7216.     }
  7217.   }
  7218.   
  7219.   conditional_skip (ip, skip, T_IF, control_macro, op);
  7220.   return 0;
  7221. }
  7222.  
  7223. /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
  7224.    If this is a #ifndef starting at the beginning of a file,
  7225.    CONTROL_MACRO is the macro name tested by the #ifndef.
  7226.    Otherwise, CONTROL_MACRO is 0.  */
  7227.  
  7228. static void
  7229. conditional_skip (ip, skip, type, control_macro, op)
  7230.      FILE_BUF *ip;
  7231.      int skip;
  7232.      enum node_type type;
  7233.      U_CHAR *control_macro;
  7234.      FILE_BUF *op;
  7235. {
  7236.   IF_STACK_FRAME *temp;
  7237.  
  7238.   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7239.   temp->fname = ip->nominal_fname;
  7240.   temp->lineno = ip->lineno;
  7241.   temp->next = if_stack;
  7242.   temp->control_macro = control_macro;
  7243.   if_stack = temp;
  7244.  
  7245.   if_stack->type = type;
  7246.  
  7247.   if (skip != 0) {
  7248.     skip_if_group (ip, 0, op);
  7249.     return;
  7250.   } else {
  7251.     ++if_stack->if_succeeded;
  7252.     output_line_directive (ip, &outbuf, 1, same_file);
  7253.   }
  7254. }
  7255.  
  7256. /*
  7257.  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
  7258.  * leaves input ptr at the sharp sign found.
  7259.  * If ANY is nonzero, return at next directive of any sort.
  7260.  */
  7261. static void
  7262. skip_if_group (ip, any, op)
  7263.      FILE_BUF *ip;
  7264.      int any;
  7265.      FILE_BUF *op;
  7266. {
  7267.   register U_CHAR *bp = ip->bufp, *cp;
  7268.   register U_CHAR *endb = ip->buf + ip->length;
  7269.   struct directive *kt;
  7270.   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
  7271.   U_CHAR *beg_of_line = bp;
  7272.   register int ident_length;
  7273.   U_CHAR *ident, *after_ident;
  7274.   /* Save info about where the group starts.  */
  7275.   U_CHAR *beg_of_group = bp;
  7276.   int beg_lineno = ip->lineno;
  7277.  
  7278.   if (output_conditionals && op != 0) {
  7279.     char *ptr = "#failed\n";
  7280.     int len = strlen (ptr);
  7281.  
  7282.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7283.       {
  7284.     *op->bufp++ = '\n';
  7285.     op->lineno++;
  7286.       }
  7287.     check_expand (op, len);
  7288.     bcopy (ptr, (char *) op->bufp, len);
  7289.     op->bufp += len;
  7290.     op->lineno++;
  7291.     output_line_directive (ip, op, 1, 0);
  7292.   }
  7293.  
  7294.   while (bp < endb) {
  7295.     switch (*bp++) {
  7296.     case '/':            /* possible comment */
  7297.       if (*bp == '\\' && bp[1] == '\n')
  7298.     newline_fix (bp);
  7299.       if (*bp == '*'
  7300.       || (cplusplus_comments && *bp == '/')) {
  7301.     ip->bufp = ++bp;
  7302.     bp = skip_to_end_of_comment (ip, &ip->lineno, 0);
  7303.       }
  7304.       break;
  7305.     case '\"':
  7306.     case '\'':
  7307.       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno,
  7308.                    NULL_PTR, NULL_PTR);
  7309.       break;
  7310.     case '\\':
  7311.       /* Char after backslash loses its special meaning.  */
  7312.       if (bp < endb) {
  7313.     if (*bp == '\n')
  7314.       ++ip->lineno;        /* But do update the line-count.  */
  7315.     bp++;
  7316.       }
  7317.       break;
  7318.     case '\n':
  7319.       ++ip->lineno;
  7320.       beg_of_line = bp;
  7321.       break;
  7322.     case '%':
  7323.       if (beg_of_line == 0 || traditional)
  7324.     break;
  7325.       ip->bufp = bp - 1;
  7326.       while (bp[0] == '\\' && bp[1] == '\n')
  7327.     bp += 2;
  7328.       if (*bp == ':')
  7329.     goto sharp_token;
  7330.       break;
  7331.     case '#':
  7332.       /* # keyword: a # must be first nonblank char on the line */
  7333.       if (beg_of_line == 0)
  7334.     break;
  7335.       ip->bufp = bp - 1;
  7336.     sharp_token:
  7337.       /* Scan from start of line, skipping whitespace, comments
  7338.      and backslash-newlines, and see if we reach this #.
  7339.      If not, this # is not special.  */
  7340.       bp = beg_of_line;
  7341.       /* If -traditional, require # to be at beginning of line.  */
  7342.       if (!traditional) {
  7343.     while (1) {
  7344.       if (is_hor_space[*bp])
  7345.         bp++;
  7346.       else if (*bp == '\\' && bp[1] == '\n')
  7347.         bp += 2;
  7348.       else if (*bp == '/' && bp[1] == '*') {
  7349.         bp += 2;
  7350.         while (!(*bp == '*' && bp[1] == '/'))
  7351.           bp++;
  7352.         bp += 2;
  7353.       }
  7354.       /* There is no point in trying to deal with C++ // comments here,
  7355.          because if there is one, then this # must be part of the
  7356.          comment and we would never reach here.  */
  7357.       else break;
  7358.     }
  7359.       }
  7360.       if (bp != ip->bufp) {
  7361.     bp = ip->bufp + 1;    /* Reset bp to after the #.  */
  7362.     break;
  7363.       }
  7364.  
  7365.       bp = ip->bufp + 1;    /* Point after the '#' */
  7366.       if (ip->bufp[0] == '%') {
  7367.     /* Skip past the ':' again.  */
  7368.     while (*bp == '\\') {
  7369.       ip->lineno++;
  7370.       bp += 2;
  7371.     }
  7372.     bp++;
  7373.       }
  7374.  
  7375.       /* Skip whitespace and \-newline.  */
  7376.       while (1) {
  7377.     if (is_hor_space[*bp])
  7378.       bp++;
  7379.     else if (*bp == '\\' && bp[1] == '\n')
  7380.       bp += 2;
  7381.     else if (*bp == '/' && bp[1] == '*') {
  7382.       bp += 2;
  7383.       while (!(*bp == '*' && bp[1] == '/')) {
  7384.         if (*bp == '\n')
  7385.           ip->lineno++;
  7386.         bp++;
  7387.       }
  7388.       bp += 2;
  7389.     } else if (cplusplus_comments && *bp == '/' && bp[1] == '/') {
  7390.       bp += 2;
  7391.       while (bp[-1] == '\\' || *bp != '\n') {
  7392.         if (*bp == '\n')
  7393.           ip->lineno++;
  7394.         bp++;
  7395.       }
  7396.         }
  7397.     else break;
  7398.       }
  7399.  
  7400.       cp = bp;
  7401.  
  7402.       /* Now find end of directive name.
  7403.      If we encounter a backslash-newline, exchange it with any following
  7404.      symbol-constituents so that we end up with a contiguous name.  */
  7405.  
  7406.       while (1) {
  7407.     if (is_idchar[*bp])
  7408.       bp++;
  7409.     else {
  7410.       if (*bp == '\\' && bp[1] == '\n')
  7411.         name_newline_fix (bp);
  7412.       if (is_idchar[*bp])
  7413.         bp++;
  7414.       else break;
  7415.     }
  7416.       }
  7417.       ident_length = bp - cp;
  7418.       ident = cp;
  7419.       after_ident = bp;
  7420.  
  7421.       /* A line of just `#' becomes blank.  */
  7422.  
  7423.       if (ident_length == 0 && *after_ident == '\n') {
  7424.     continue;
  7425.       }
  7426.  
  7427.       if (ident_length == 0 || !is_idstart[*ident]) {
  7428.     U_CHAR *p = ident;
  7429.     while (is_idchar[*p]) {
  7430.       if (*p < '0' || *p > '9')
  7431.         break;
  7432.       p++;
  7433.     }
  7434.     /* Handle # followed by a line number.  */
  7435.     if (p != ident && !is_idchar[*p]) {
  7436.       if (pedantic)
  7437.         pedwarn ("`#' followed by integer");
  7438.       continue;
  7439.     }
  7440.  
  7441.     /* Avoid error for `###' and similar cases unless -pedantic.  */
  7442.     if (p == ident) {
  7443.       while (*p == '#' || is_hor_space[*p]) p++;
  7444.       if (*p == '\n') {
  7445.         if (pedantic && !lang_asm)
  7446.           pedwarn ("invalid preprocessing directive");
  7447.         continue;
  7448.       }
  7449.     }
  7450.  
  7451.     if (!lang_asm && pedantic)
  7452.       pedwarn ("invalid preprocessing directive name");
  7453.     continue;
  7454.       }
  7455.  
  7456.       for (kt = directive_table; kt->length >= 0; kt++) {
  7457.     IF_STACK_FRAME *temp;
  7458.     if (ident_length == kt->length
  7459.         && bcmp (cp, kt->name, kt->length) == 0) {
  7460.       /* If we are asked to return on next directive, do so now.  */
  7461.       if (any)
  7462.         goto done;
  7463.  
  7464.       switch (kt->type) {
  7465.       case T_IF:
  7466.       case T_IFDEF:
  7467.       case T_IFNDEF:
  7468.         temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
  7469.         temp->next = if_stack;
  7470.         if_stack = temp;
  7471.         temp->lineno = ip->lineno;
  7472.         temp->fname = ip->nominal_fname;
  7473.         temp->type = kt->type;
  7474.         break;
  7475.       case T_ELSE:
  7476.       case T_ENDIF:
  7477.         if (pedantic && if_stack != save_if_stack)
  7478.           validate_else (bp);
  7479.       case T_ELIF:
  7480.         if (if_stack == instack[indepth].if_stack) {
  7481.           error ("`#%s' not within a conditional", kt->name);
  7482.           break;
  7483.         }
  7484.         else if (if_stack == save_if_stack)
  7485.           goto done;        /* found what we came for */
  7486.  
  7487.         if (kt->type != T_ENDIF) {
  7488.           if (if_stack->type == T_ELSE)
  7489.         error ("`#else' or `#elif' after `#else'");
  7490.           if_stack->type = kt->type;
  7491.           break;
  7492.         }
  7493.  
  7494.         temp = if_stack;
  7495.         if_stack = if_stack->next;
  7496.         free (temp);
  7497.         break;
  7498.  
  7499.        default:
  7500.         break;
  7501.       }
  7502.       break;
  7503.     }
  7504.       }
  7505.       /* Don't let erroneous code go by.  */
  7506.       if (kt->length < 0 && !lang_asm && pedantic)
  7507.     pedwarn ("invalid preprocessing directive name");
  7508.     }
  7509.   }
  7510.  
  7511.   ip->bufp = bp;
  7512.   /* after this returns, rescan will exit because ip->bufp
  7513.      now points to the end of the buffer.
  7514.      rescan is responsible for the error message also.  */
  7515.  
  7516.  done:
  7517.   if (output_conditionals && op != 0) {
  7518.     char *ptr = "#endfailed\n";
  7519.     int len = strlen (ptr);
  7520.  
  7521.     if (op->bufp > op->buf && op->bufp[-1] != '\n')
  7522.       {
  7523.     *op->bufp++ = '\n';
  7524.     op->lineno++;
  7525.       }
  7526.     check_expand (op, beg_of_line - beg_of_group);
  7527.     bcopy ((char *) beg_of_group, (char *) op->bufp,
  7528.        beg_of_line - beg_of_group);
  7529.     op->bufp += beg_of_line - beg_of_group;
  7530.     op->lineno += ip->lineno - beg_lineno;
  7531.     check_expand (op, len);
  7532.     bcopy (ptr, (char *) op->bufp, len);
  7533.     op->bufp += len;
  7534.     op->lineno++;
  7535.   }
  7536. }
  7537.  
  7538. /*
  7539.  * handle a #else directive.  Do this by just continuing processing
  7540.  * without changing  if_stack ;  this is so that the error message
  7541.  * for missing #endif's etc. will point to the original #if.  It
  7542.  * is possible that something different would be better.
  7543.  */
  7544.  
  7545. static int
  7546. do_else (buf, limit, op, keyword)
  7547.      U_CHAR *buf, *limit;
  7548.      FILE_BUF *op;
  7549.      struct directive *keyword;
  7550. {
  7551.   FILE_BUF *ip = &instack[indepth];
  7552.  
  7553.   if (pedantic) {
  7554.     SKIP_WHITE_SPACE (buf);
  7555.     if (buf != limit)
  7556.       pedwarn ("text following `#else' violates ANSI standard");
  7557.   }
  7558.  
  7559.   if (if_stack == instack[indepth].if_stack) {
  7560.     error ("`#else' not within a conditional");
  7561.     return 0;
  7562.   } else {
  7563.     /* #ifndef can't have its special treatment for containing the whole file
  7564.        if it has a #else clause.  */
  7565.     if_stack->control_macro = 0;
  7566.  
  7567.     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
  7568.       error ("`#else' after `#else'");
  7569.       fprintf (stderr, " (matches line %d", if_stack->lineno);
  7570.       if (strcmp (if_stack->fname, ip->nominal_fname) != 0)
  7571.     fprintf (stderr, ", file %s", if_stack->fname);
  7572.       fprintf (stderr, ")\n");
  7573.     }
  7574.     if_stack->type = T_ELSE;
  7575.   }
  7576.  
  7577.   if (if_stack->if_succeeded)
  7578.     skip_if_group (ip, 0, op);
  7579.   else {
  7580.     ++if_stack->if_succeeded;    /* continue processing input */
  7581.     output_line_directive (ip, op, 1, same_file);
  7582.   }
  7583.   return 0;
  7584. }
  7585.  
  7586. /*
  7587.  * unstack after #endif directive
  7588.  */
  7589.  
  7590. static int
  7591. do_endif (buf, limit, op, keyword)
  7592.      U_CHAR *buf, *limit;
  7593.      FILE_BUF *op;
  7594.      struct directive *keyword;
  7595. {
  7596.   if (pedantic) {
  7597.     SKIP_WHITE_SPACE (buf);
  7598.     if (buf != limit)
  7599.       pedwarn ("text following `#endif' violates ANSI standard");
  7600.   }
  7601.  
  7602.   if (if_stack == instack[indepth].if_stack)
  7603.     error ("unbalanced `#endif'");
  7604.   else {
  7605.     IF_STACK_FRAME *temp = if_stack;
  7606.     if_stack = if_stack->next;
  7607.     if (temp->control_macro != 0) {
  7608.       /* This #endif matched a #ifndef at the start of the file.
  7609.      See if it is at the end of the file.  */
  7610.       FILE_BUF *ip = &instack[indepth];
  7611.       U_CHAR *p = ip->bufp;
  7612.       U_CHAR *ep = ip->buf + ip->length;
  7613.  
  7614.       while (p != ep) {
  7615.     U_CHAR c = *p++;
  7616.     if (!is_space[c]) {
  7617.       if (c == '/'
  7618.           && (*p == '*' || (cplusplus_comments && *p == '/'))) {
  7619.         /* Skip this comment.  */
  7620.         int junk = 0;
  7621.         U_CHAR *save_bufp = ip->bufp;
  7622.         ip->bufp = p + 1;
  7623.         p = skip_to_end_of_comment (ip, &junk, 1);
  7624.         ip->bufp = save_bufp;
  7625.       } else
  7626.         goto fail;
  7627.     }
  7628.       }
  7629.       /* If we get here, this #endif ends a #ifndef
  7630.      that contains all of the file (aside from whitespace).
  7631.      Arrange not to include the file again
  7632.      if the macro that was tested is defined.
  7633.  
  7634.      Do not do this for the top-level file in a -include or any
  7635.      file in a -imacros.  */
  7636.       if (indepth != 0
  7637.       && ! (indepth == 1 && no_record_file)
  7638.       && ! (no_record_file && no_output))
  7639.     record_control_macro (ip->fname, temp->control_macro);
  7640.     fail: ;
  7641.     }
  7642.     free (temp);
  7643.     output_line_directive (&instack[indepth], op, 1, same_file);
  7644.   }
  7645.   return 0;
  7646. }
  7647.  
  7648. /* When an #else or #endif is found while skipping failed conditional,
  7649.    if -pedantic was specified, this is called to warn about text after
  7650.    the directive name.  P points to the first char after the directive name.  */
  7651.  
  7652. static void
  7653. validate_else (p)
  7654.      register U_CHAR *p;
  7655. {
  7656.   /* Advance P over whitespace and comments.  */
  7657.   while (1) {
  7658.     if (*p == '\\' && p[1] == '\n')
  7659.       p += 2;
  7660.     if (is_hor_space[*p])
  7661.       p++;
  7662.     else if (*p == '/') {
  7663.       if (p[1] == '\\' && p[2] == '\n')
  7664.     newline_fix (p + 1);
  7665.       if (p[1] == '*') {
  7666.     p += 2;
  7667.     /* Don't bother warning about unterminated comments
  7668.        since that will happen later.  Just be sure to exit.  */
  7669.     while (*p) {
  7670.       if (p[1] == '\\' && p[2] == '\n')
  7671.         newline_fix (p + 1);
  7672.       if (*p == '*' && p[1] == '/') {
  7673.         p += 2;
  7674.         break;
  7675.       }
  7676.       p++;
  7677.     }
  7678.       }
  7679.       else if (cplusplus_comments && p[1] == '/') {
  7680.     p += 2;
  7681.     while (*p && (*p != '\n' || p[-1] == '\\'))
  7682.       p++;
  7683.       }
  7684.     } else break;
  7685.   }
  7686.   if (*p && *p != '\n')
  7687.     pedwarn ("text following `#else' or `#endif' violates ANSI standard");
  7688. }
  7689.  
  7690. /* Skip a comment, assuming the input ptr immediately follows the
  7691.    initial slash-star.  Bump *LINE_COUNTER for each newline.
  7692.    (The canonical line counter is &ip->lineno.)
  7693.    Don't use this routine (or the next one) if bumping the line
  7694.    counter is not sufficient to deal with newlines in the string.
  7695.  
  7696.    If NOWARN is nonzero, don't warn about slash-star inside a comment.
  7697.    This feature is useful when processing a comment that is going to be
  7698.    processed or was processed at another point in the preprocessor,
  7699.    to avoid a duplicate warning.  Likewise for unterminated comment errors.  */
  7700.  
  7701. static U_CHAR *
  7702. skip_to_end_of_comment (ip, line_counter, nowarn)
  7703.      register FILE_BUF *ip;
  7704.      int *line_counter;        /* place to remember newlines, or NULL */
  7705.      int nowarn;
  7706. {
  7707.   register U_CHAR *limit = ip->buf + ip->length;
  7708.   register U_CHAR *bp = ip->bufp;
  7709.   FILE_BUF *op = &outbuf;    /* JF */
  7710.   int output = put_out_comments && !line_counter;
  7711.   int start_line = line_counter ? *line_counter : 0;
  7712.  
  7713.     /* JF this line_counter stuff is a crock to make sure the
  7714.        comment is only put out once, no matter how many times
  7715.        the comment is skipped.  It almost works */
  7716.   if (output) {
  7717.     *op->bufp++ = '/';
  7718.     *op->bufp++ = '*';
  7719.   }
  7720.   if (cplusplus_comments && bp[-1] == '/') {
  7721.     if (output) {
  7722.       while (bp < limit) {
  7723.     *op->bufp++ = *bp;
  7724.     if (*bp == '\n' && bp[-1] != '\\')
  7725.       break;
  7726.     if (*bp == '\n') {
  7727.       ++*line_counter;
  7728.       ++op->lineno;
  7729.     }
  7730.     bp++;
  7731.       }
  7732.       op->bufp[-1] = '*';
  7733.       *op->bufp++ = '/';
  7734.       *op->bufp++ = '\n';
  7735.     } else {
  7736.       while (bp < limit) {
  7737.     if (bp[-1] != '\\' && *bp == '\n') {
  7738.       break;
  7739.     } else {
  7740.       if (*bp == '\n' && line_counter)
  7741.         ++*line_counter;
  7742.       bp++;
  7743.     }
  7744.       }
  7745.     }
  7746.     ip->bufp = bp;
  7747.     return bp;
  7748.   }
  7749.   while (bp < limit) {
  7750.     if (output)
  7751.       *op->bufp++ = *bp;
  7752.     switch (*bp++) {
  7753.     case '/':
  7754.       if (warn_comments && !nowarn && bp < limit && *bp == '*')
  7755.     warning ("`/*' within comment");
  7756.       break;
  7757.     case '\n':
  7758.       /* If this is the end of the file, we have an unterminated comment.
  7759.      Don't swallow the newline.  We are guaranteed that there will be a
  7760.      trailing newline and various pieces assume it's there.  */
  7761.       if (bp == limit)
  7762.     {
  7763.       --bp;
  7764.       --limit;
  7765.       break;
  7766.     }
  7767.       if (line_counter != NULL)
  7768.     ++*line_counter;
  7769.       if (output)
  7770.     ++op->lineno;
  7771.       break;
  7772.     case '*':
  7773.       if (*bp == '\\' && bp[1] == '\n')
  7774.     newline_fix (bp);
  7775.       if (*bp == '/') {
  7776.         if (output)
  7777.       *op->bufp++ = '/';
  7778.     ip->bufp = ++bp;
  7779.     return bp;
  7780.       }
  7781.       break;
  7782.     }
  7783.   }
  7784.  
  7785.   if (!nowarn)
  7786.     error_with_line (line_for_error (start_line), "unterminated comment");
  7787.   ip->bufp = bp;
  7788.   return bp;
  7789. }
  7790.  
  7791. /*
  7792.  * Skip over a quoted string.  BP points to the opening quote.
  7793.  * Returns a pointer after the closing quote.  Don't go past LIMIT.
  7794.  * START_LINE is the line number of the starting point (but it need
  7795.  * not be valid if the starting point is inside a macro expansion).
  7796.  *
  7797.  * The input stack state is not changed.
  7798.  *
  7799.  * If COUNT_NEWLINES is nonzero, it points to an int to increment
  7800.  * for each newline passed.
  7801.  *
  7802.  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
  7803.  * if we pass a backslash-newline.
  7804.  *
  7805.  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
  7806.  */
  7807. static U_CHAR *
  7808. skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
  7809.      register U_CHAR *bp;
  7810.      register U_CHAR *limit;
  7811.      int start_line;
  7812.      int *count_newlines;
  7813.      int *backslash_newlines_p;
  7814.      int *eofp;
  7815. {
  7816.   register U_CHAR c, match;
  7817.  
  7818.   match = *bp++;
  7819.   while (1) {
  7820.     if (bp >= limit) {
  7821.       error_with_line (line_for_error (start_line),
  7822.                "unterminated string or character constant");
  7823.       error_with_line (multiline_string_line,
  7824.                "possible real start of unterminated constant");
  7825.       multiline_string_line = 0;
  7826.       if (eofp)
  7827.     *eofp = 1;
  7828.       break;
  7829.     }
  7830.     c = *bp++;
  7831.     if (c == '\\') {
  7832.       while (*bp == '\\' && bp[1] == '\n') {
  7833.     if (backslash_newlines_p)
  7834.       *backslash_newlines_p = 1;
  7835.     if (count_newlines)
  7836.       ++*count_newlines;
  7837.     bp += 2;
  7838.       }
  7839.       if (*bp == '\n' && count_newlines) {
  7840.     if (backslash_newlines_p)
  7841.       *backslash_newlines_p = 1;
  7842.     ++*count_newlines;
  7843.       }
  7844.       bp++;
  7845.     } else if (c == '\n') {
  7846.       if (traditional) {
  7847.      /* Unterminated strings and character constants are 'valid'.  */
  7848.      bp--;    /* Don't consume the newline. */
  7849.      if (eofp)
  7850.        *eofp = 1;
  7851.      break;
  7852.       }
  7853.       if (pedantic || match == '\'') {
  7854.     error_with_line (line_for_error (start_line),
  7855.              "unterminated string or character constant");
  7856.     bp--;
  7857.     if (eofp)
  7858.       *eofp = 1;
  7859.     break;
  7860.       }
  7861.       /* If not traditional, then allow newlines inside strings.  */
  7862.       if (count_newlines)
  7863.     ++*count_newlines;
  7864.       if (multiline_string_line == 0)
  7865.     multiline_string_line = start_line;
  7866.     } else if (c == match)
  7867.       break;
  7868.   }
  7869.   return bp;
  7870. }
  7871.  
  7872. /* Place into DST a quoted string representing the string SRC.
  7873.    Return the address of DST's terminating null.  */
  7874. static char *
  7875. quote_string (dst, src)
  7876.      char *dst, *src;
  7877. {
  7878.   U_CHAR c;
  7879.  
  7880.   *dst++ = '\"';
  7881.   for (;;)
  7882.     switch ((c = *src++))
  7883.       {
  7884.       default:
  7885.         if (isprint (c))
  7886.       *dst++ = c;
  7887.     else
  7888.       {
  7889.         sprintf (dst, "\\%03o", c);
  7890.         dst += 4;
  7891.       }
  7892.     break;
  7893.  
  7894.       case '\"':
  7895.       case '\\':
  7896.     *dst++ = '\\';
  7897.     *dst++ = c;
  7898.     break;
  7899.       
  7900.       case '\0':
  7901.     *dst++ = '\"';
  7902.     *dst = '\0';
  7903.     return dst;
  7904.       }
  7905. }
  7906.  
  7907. /* Skip across a group of balanced parens, starting from IP->bufp.
  7908.    IP->bufp is updated.  Use this with IP->bufp pointing at an open-paren.
  7909.  
  7910.    This does not handle newlines, because it's used for the arg of #if,
  7911.    where there aren't any newlines.  Also, backslash-newline can't appear.  */
  7912.  
  7913. static U_CHAR *
  7914. skip_paren_group (ip)
  7915.      register FILE_BUF *ip;
  7916. {
  7917.   U_CHAR *limit = ip->buf + ip->length;
  7918.   U_CHAR *p = ip->bufp;
  7919.   int depth = 0;
  7920.   int lines_dummy = 0;
  7921.  
  7922.   while (p != limit) {
  7923.     int c = *p++;
  7924.     switch (c) {
  7925.     case '(':
  7926.       depth++;
  7927.       break;
  7928.  
  7929.     case ')':
  7930.       depth--;
  7931.       if (depth == 0)
  7932.     return ip->bufp = p;
  7933.       break;
  7934.  
  7935.     case '/':
  7936.       if (*p == '*') {
  7937.     ip->bufp = p;
  7938.     p = skip_to_end_of_comment (ip, &lines_dummy, 0);
  7939.     p = ip->bufp;
  7940.       }
  7941.  
  7942.     case '"':
  7943.     case '\'':
  7944.       {
  7945.     int eofp = 0;
  7946.     p = skip_quoted_string (p - 1, limit, 0, NULL_PTR, NULL_PTR, &eofp);
  7947.     if (eofp)
  7948.       return ip->bufp = p;
  7949.       }
  7950.       break;
  7951.     }
  7952.   }
  7953.  
  7954.   ip->bufp = p;
  7955.   return p;
  7956. }
  7957.  
  7958. /*
  7959.  * write out a #line directive, for instance, after an #include file.
  7960.  * If CONDITIONAL is nonzero, we can omit the #line if it would
  7961.  * appear to be a no-op, and we can output a few newlines instead
  7962.  * if we want to increase the line number by a small amount.
  7963.  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
  7964.  */
  7965.  
  7966. static void
  7967. output_line_directive (ip, op, conditional, file_change)
  7968.      FILE_BUF *ip, *op;
  7969.      int conditional;
  7970.      enum file_change_code file_change;
  7971. {
  7972.   int len;
  7973.   char *line_directive_buf, *line_end;
  7974.  
  7975.   if (no_line_directives
  7976.       || ip->fname == NULL
  7977.       || no_output) {
  7978.     op->lineno = ip->lineno;
  7979.     return;
  7980.   }
  7981.  
  7982.   if (conditional) {
  7983.     if (ip->lineno == op->lineno)
  7984.       return;
  7985.  
  7986.     /* If the inherited line number is a little too small,
  7987.        output some newlines instead of a #line directive.  */
  7988.     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
  7989.       check_expand (op, 10);
  7990.       while (ip->lineno > op->lineno) {
  7991.     *op->bufp++ = '\n';
  7992.     op->lineno++;
  7993.       }
  7994.       return;
  7995.     }
  7996.   }
  7997.  
  7998.   /* Don't output a line number of 0 if we can help it.  */
  7999.   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
  8000.       && *ip->bufp == '\n') {
  8001.     ip->lineno++;
  8002.     ip->bufp++;
  8003.   }
  8004.  
  8005.   line_directive_buf = (char *) alloca (4 * strlen (ip->nominal_fname) + 100);
  8006.   sprintf (line_directive_buf, "# %d ", ip->lineno);
  8007.   line_end = quote_string (line_directive_buf + strlen (line_directive_buf),
  8008.                ip->nominal_fname);
  8009.   if (file_change != same_file) {
  8010.     *line_end++ = ' ';
  8011.     *line_end++ = file_change == enter_file ? '1' : '2';
  8012.   }
  8013.   /* Tell cc1 if following text comes from a system header file.  */
  8014.   if (ip->system_header_p) {
  8015.     *line_end++ = ' ';
  8016.     *line_end++ = '3';
  8017.   }
  8018. #ifndef NO_IMPLICIT_EXTERN_C
  8019.   /* Tell cc1plus if following text should be treated as C.  */
  8020.   if (ip->system_header_p == 2 && cplusplus) {
  8021.     *line_end++ = ' ';
  8022.     *line_end++ = '4';
  8023.   }
  8024. #endif
  8025.   *line_end++ = '\n';
  8026.   len = line_end - line_directive_buf;
  8027.   check_expand (op, len + 1);
  8028.   if (op->bufp > op->buf && op->bufp[-1] != '\n')
  8029.     *op->bufp++ = '\n';
  8030.   bcopy ((char *) line_directive_buf, (char *) op->bufp, len);
  8031.   op->bufp += len;
  8032.   op->lineno = ip->lineno;
  8033. }
  8034.  
  8035. /* This structure represents one parsed argument in a macro call.
  8036.    `raw' points to the argument text as written (`raw_length' is its length).
  8037.    `expanded' points to the argument's macro-expansion
  8038.    (its length is `expand_length').
  8039.    `stringified_length' is the length the argument would have
  8040.    if stringified.
  8041.    `use_count' is the number of times this macro arg is substituted
  8042.    into the macro.  If the actual use count exceeds 10, 
  8043.    the value stored is 10.
  8044.    `free1' and `free2', if nonzero, point to blocks to be freed
  8045.    when the macro argument data is no longer needed.  */
  8046.  
  8047. struct argdata {
  8048.   U_CHAR *raw, *expanded;
  8049.   int raw_length, expand_length;
  8050.   int stringified_length;
  8051.   U_CHAR *free1, *free2;
  8052.   char newlines;
  8053.   char use_count;
  8054. };
  8055.  
  8056. /* Expand a macro call.
  8057.    HP points to the symbol that is the macro being called.
  8058.    Put the result of expansion onto the input stack
  8059.    so that subsequent input by our caller will use it.
  8060.  
  8061.    If macro wants arguments, caller has already verified that
  8062.    an argument list follows; arguments come from the input stack.  */
  8063.  
  8064. static void
  8065. macroexpand (hp, op)
  8066.      HASHNODE *hp;
  8067.      FILE_BUF *op;
  8068. {
  8069.   int nargs;
  8070.   DEFINITION *defn = hp->value.defn;
  8071.   register U_CHAR *xbuf;
  8072.   int xbuf_len;
  8073.   int start_line = instack[indepth].lineno;
  8074.   int rest_args, rest_zero;
  8075.  
  8076.   CHECK_DEPTH (return;);
  8077.  
  8078.   /* it might not actually be a macro.  */
  8079.   if (hp->type != T_MACRO) {
  8080.     special_symbol (hp, op);
  8081.     return;
  8082.   }
  8083.  
  8084.   /* This macro is being used inside a #if, which means it must be */
  8085.   /* recorded as a precondition.  */
  8086.   if (pcp_inside_if && pcp_outfile && defn->predefined)
  8087.     dump_single_macro (hp, pcp_outfile);
  8088.   
  8089.   nargs = defn->nargs;
  8090.  
  8091.   if (nargs >= 0) {
  8092.     register int i;
  8093.     struct argdata *args;
  8094.     char *parse_error = 0;
  8095.  
  8096.     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
  8097.  
  8098.     for (i = 0; i < nargs; i++) {
  8099.       args[i].raw = (U_CHAR *) "";
  8100.       args[i].expanded = 0;
  8101.       args[i].raw_length = args[i].expand_length
  8102.     = args[i].stringified_length = 0;
  8103.       args[i].free1 = args[i].free2 = 0;
  8104.       args[i].use_count = 0;
  8105.     }
  8106.  
  8107.     /* Parse all the macro args that are supplied.  I counts them.
  8108.        The first NARGS args are stored in ARGS.
  8109.        The rest are discarded.
  8110.        If rest_args is set then we assume macarg absorbed the rest of the args.
  8111.        */
  8112.     i = 0;
  8113.     rest_args = 0;
  8114.     do {
  8115.       /* Discard the open-parenthesis or comma before the next arg.  */
  8116.       ++instack[indepth].bufp;
  8117.       if (rest_args)
  8118.     continue;
  8119.       if (i < nargs || (nargs == 0 && i == 0)) {
  8120.     /* if we are working on last arg which absorbs rest of args... */
  8121.     if (i == nargs - 1 && defn->rest_args)
  8122.       rest_args = 1;
  8123.     parse_error = macarg (&args[i], rest_args);
  8124.       }
  8125.       else
  8126.     parse_error = macarg (NULL_PTR, 0);
  8127.       if (parse_error) {
  8128.     error_with_line (line_for_error (start_line), parse_error);
  8129.     break;
  8130.       }
  8131.       i++;
  8132.     } while (*instack[indepth].bufp != ')');
  8133.  
  8134.     /* If we got one arg but it was just whitespace, call that 0 args.  */
  8135.     if (i == 1) {
  8136.       register U_CHAR *bp = args[0].raw;
  8137.       register U_CHAR *lim = bp + args[0].raw_length;
  8138.       /* cpp.texi says for foo ( ) we provide one argument.
  8139.      However, if foo wants just 0 arguments, treat this as 0.  */
  8140.       if (nargs == 0)
  8141.     while (bp != lim && is_space[*bp]) bp++;
  8142.       if (bp == lim)
  8143.     i = 0;
  8144.     }
  8145.  
  8146.     /* Don't output an error message if we have already output one for
  8147.        a parse error above.  */
  8148.     rest_zero = 0;
  8149.     if (nargs == 0 && i > 0) {
  8150.       if (! parse_error)
  8151.     error ("arguments given to macro `%s'", hp->name);
  8152.     } else if (i < nargs) {
  8153.       /* traditional C allows foo() if foo wants one argument.  */
  8154.       if (nargs == 1 && i == 0 && traditional)
  8155.     ;
  8156.       /* the rest args token is allowed to absorb 0 tokens */
  8157.       else if (i == nargs - 1 && defn->rest_args)
  8158.     rest_zero = 1;
  8159.       else if (parse_error)
  8160.     ;
  8161.       else if (i == 0)
  8162.     error ("macro `%s' used without args", hp->name);
  8163.       else if (i == 1)
  8164.     error ("macro `%s' used with just one arg", hp->name);
  8165.       else
  8166.     error ("macro `%s' used with only %d args", hp->name, i);
  8167.     } else if (i > nargs) {
  8168.       if (! parse_error)
  8169.     error ("macro `%s' used with too many (%d) args", hp->name, i);
  8170.     }
  8171.  
  8172.     /* Swallow the closeparen.  */
  8173.     ++instack[indepth].bufp;
  8174.  
  8175.     /* If macro wants zero args, we parsed the arglist for checking only.
  8176.        Read directly from the macro definition.  */
  8177.     if (nargs == 0) {
  8178.       xbuf = defn->expansion;
  8179.       xbuf_len = defn->length;
  8180.     } else {
  8181.       register U_CHAR *exp = defn->expansion;
  8182.       register int offset;    /* offset in expansion,
  8183.                    copied a piece at a time */
  8184.       register int totlen;    /* total amount of exp buffer filled so far */
  8185.  
  8186.       register struct reflist *ap, *last_ap;
  8187.  
  8188.       /* Macro really takes args.  Compute the expansion of this call.  */
  8189.  
  8190.       /* Compute length in characters of the macro's expansion.
  8191.      Also count number of times each arg is used.  */
  8192.       xbuf_len = defn->length;
  8193.       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  8194.     if (ap->stringify)
  8195.       xbuf_len += args[ap->argno].stringified_length;
  8196.     else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional)
  8197.       /* Add 4 for two newline-space markers to prevent
  8198.          token concatenation.  */
  8199.       xbuf_len += args[ap->argno].raw_length + 4;
  8200.     else {
  8201.       /* We have an ordinary (expanded) occurrence of the arg.
  8202.          So compute its expansion, if we have not already.  */
  8203.       if (args[ap->argno].expanded == 0) {
  8204.         FILE_BUF obuf;
  8205.         obuf = expand_to_temp_buffer (args[ap->argno].raw,
  8206.                       args[ap->argno].raw + args[ap->argno].raw_length,
  8207.                       1, 0);
  8208.  
  8209.         args[ap->argno].expanded = obuf.buf;
  8210.         args[ap->argno].expand_length = obuf.length;
  8211.         args[ap->argno].free2 = obuf.buf;
  8212.       }
  8213.  
  8214.       /* Add 4 for two newline-space markers to prevent
  8215.          token concatenation.  */
  8216.       xbuf_len += args[ap->argno].expand_length + 4;
  8217.     }
  8218.     if (args[ap->argno].use_count < 10)
  8219.       args[ap->argno].use_count++;
  8220.       }
  8221.  
  8222.       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
  8223.  
  8224.       /* Generate in XBUF the complete expansion
  8225.      with arguments substituted in.
  8226.      TOTLEN is the total size generated so far.
  8227.      OFFSET is the index in the definition
  8228.      of where we are copying from.  */
  8229.       offset = totlen = 0;
  8230.       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
  8231.        last_ap = ap, ap = ap->next) {
  8232.     register struct argdata *arg = &args[ap->argno];
  8233.     int count_before = totlen;
  8234.  
  8235.     /* Add chars to XBUF.  */
  8236.     for (i = 0; i < ap->nchars; i++, offset++)
  8237.       xbuf[totlen++] = exp[offset];
  8238.  
  8239.     /* If followed by an empty rest arg with concatenation,
  8240.        delete the last run of nonwhite chars.  */
  8241.     if (rest_zero && totlen > count_before
  8242.         && ((ap->rest_args && ap->raw_before != 0)
  8243.         || (last_ap != NULL && last_ap->rest_args
  8244.             && last_ap->raw_after != 0))) {
  8245.       /* Delete final whitespace.  */
  8246.       while (totlen > count_before && is_space[xbuf[totlen - 1]]) {
  8247.         totlen--;
  8248.       }
  8249.  
  8250.       /* Delete the nonwhites before them.  */
  8251.       while (totlen > count_before && ! is_space[xbuf[totlen - 1]]) {
  8252.         totlen--;
  8253.       }
  8254.     }
  8255.  
  8256.     if (ap->stringify != 0) {
  8257.       int arglen = arg->raw_length;
  8258.       int escaped = 0;
  8259.       int in_string = 0;
  8260.       int c;
  8261.       i = 0;
  8262.       while (i < arglen
  8263.          && (c = arg->raw[i], is_space[c]))
  8264.         i++;
  8265.       while (i < arglen
  8266.          && (c = arg->raw[arglen - 1], is_space[c]))
  8267.         arglen--;
  8268.       if (!traditional)
  8269.         xbuf[totlen++] = '\"'; /* insert beginning quote */
  8270.       for (; i < arglen; i++) {
  8271.         c = arg->raw[i];
  8272.  
  8273.         /* Special markers Newline Space
  8274.            generate nothing for a stringified argument.  */
  8275.         if (c == '\n' && arg->raw[i+1] != '\n') {
  8276.           i++;
  8277.           continue;
  8278.         }
  8279.  
  8280.         /* Internal sequences of whitespace are replaced by one space
  8281.            except within an string or char token.  */
  8282.         if (! in_string
  8283.         && (c == '\n' ? arg->raw[i+1] == '\n' : is_space[c])) {
  8284.           while (1) {
  8285.         /* Note that Newline Space does occur within whitespace
  8286.            sequences; consider it part of the sequence.  */
  8287.         if (c == '\n' && is_space[arg->raw[i+1]])
  8288.           i += 2;
  8289.         else if (c != '\n' && is_space[c])
  8290.           i++;
  8291.         else break;
  8292.         c = arg->raw[i];
  8293.           }
  8294.           i--;
  8295.           c = ' ';
  8296.         }
  8297.  
  8298.         if (escaped)
  8299.           escaped = 0;
  8300.         else {
  8301.           if (c == '\\')
  8302.         escaped = 1;
  8303.           if (in_string) {
  8304.         if (c == in_string)
  8305.           in_string = 0;
  8306.           } else if (c == '\"' || c == '\'')
  8307.         in_string = c;
  8308.         }
  8309.  
  8310.         /* Escape these chars */
  8311.         if (c == '\"' || (in_string && c == '\\'))
  8312.           xbuf[totlen++] = '\\';
  8313.         if (isprint (c))
  8314.           xbuf[totlen++] = c;
  8315.         else {
  8316.           sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
  8317.           totlen += 4;
  8318.         }
  8319.       }
  8320.       if (!traditional)
  8321.         xbuf[totlen++] = '\"'; /* insert ending quote */
  8322.     } else if (ap->raw_before != 0 || ap->raw_after != 0 || traditional) {
  8323.       U_CHAR *p1 = arg->raw;
  8324.       U_CHAR *l1 = p1 + arg->raw_length;
  8325.       if (ap->raw_before != 0) {
  8326.         while (p1 != l1 && is_space[*p1]) p1++;
  8327.         while (p1 != l1 && is_idchar[*p1])
  8328.           xbuf[totlen++] = *p1++;
  8329.         /* Delete any no-reexpansion marker that follows
  8330.            an identifier at the beginning of the argument
  8331.            if the argument is concatenated with what precedes it.  */
  8332.         if (p1[0] == '\n' && p1[1] == '-')
  8333.           p1 += 2;
  8334.       } else if (!traditional) {
  8335.       /* Ordinary expanded use of the argument.
  8336.          Put in newline-space markers to prevent token pasting.  */
  8337.         xbuf[totlen++] = '\n';
  8338.         xbuf[totlen++] = ' ';
  8339.       }
  8340.       if (ap->raw_after != 0) {
  8341.         /* Arg is concatenated after: delete trailing whitespace,
  8342.            whitespace markers, and no-reexpansion markers.  */
  8343.         while (p1 != l1) {
  8344.           if (is_space[l1[-1]]) l1--;
  8345.           else if (l1[-1] == '-') {
  8346.         U_CHAR *p2 = l1 - 1;
  8347.         /* If a `-' is preceded by an odd number of newlines then it
  8348.            and the last newline are a no-reexpansion marker.  */
  8349.         while (p2 != p1 && p2[-1] == '\n') p2--;
  8350.         if ((l1 - 1 - p2) & 1) {
  8351.           l1 -= 2;
  8352.         }
  8353.         else break;
  8354.           }
  8355.           else break;
  8356.         }
  8357.       }
  8358.  
  8359.       bcopy ((char *) p1, (char *) (xbuf + totlen), l1 - p1);
  8360.       totlen += l1 - p1;
  8361.       if (!traditional && ap->raw_after == 0) {
  8362.         /* Ordinary expanded use of the argument.
  8363.            Put in newline-space markers to prevent token pasting.  */
  8364.         xbuf[totlen++] = '\n';
  8365.         xbuf[totlen++] = ' ';
  8366.       }
  8367.     } else {
  8368.       /* Ordinary expanded use of the argument.
  8369.          Put in newline-space markers to prevent token pasting.  */
  8370.       if (!traditional) {
  8371.         xbuf[totlen++] = '\n';
  8372.         xbuf[totlen++] = ' ';
  8373.       }
  8374.       bcopy ((char *) arg->expanded, (char *) (xbuf + totlen),
  8375.          arg->expand_length);
  8376.       totlen += arg->expand_length;
  8377.       if (!traditional) {
  8378.         xbuf[totlen++] = '\n';
  8379.         xbuf[totlen++] = ' ';
  8380.       }
  8381.       /* If a macro argument with newlines is used multiple times,
  8382.          then only expand the newlines once.  This avoids creating output
  8383.          lines which don't correspond to any input line, which confuses
  8384.          gdb and gcov.  */
  8385.       if (arg->use_count > 1 && arg->newlines > 0) {
  8386.         /* Don't bother doing change_newlines for subsequent
  8387.            uses of arg.  */
  8388.         arg->use_count = 1;
  8389.         arg->expand_length
  8390.           = change_newlines (arg->expanded, arg->expand_length);
  8391.       }
  8392.     }
  8393.  
  8394.     if (totlen > xbuf_len)
  8395.       abort ();
  8396.       }
  8397.  
  8398.       /* if there is anything left of the definition
  8399.      after handling the arg list, copy that in too. */
  8400.  
  8401.       for (i = offset; i < defn->length; i++) {
  8402.     /* if we've reached the end of the macro */
  8403.     if (exp[i] == ')')
  8404.       rest_zero = 0;
  8405.     if (! (rest_zero && last_ap != NULL && last_ap->rest_args
  8406.            && last_ap->raw_after != 0))
  8407.       xbuf[totlen++] = exp[i];
  8408.       }
  8409.  
  8410.       xbuf[totlen] = 0;
  8411.       xbuf_len = totlen;
  8412.  
  8413.       for (i = 0; i < nargs; i++) {
  8414.     if (args[i].free1 != 0)
  8415.       free (args[i].free1);
  8416.     if (args[i].free2 != 0)
  8417.       free (args[i].free2);
  8418.       }
  8419.     }
  8420.   } else {
  8421.     xbuf = defn->expansion;
  8422.     xbuf_len = defn->length;
  8423.   }
  8424.  
  8425.   /* Now put the expansion on the input stack
  8426.      so our caller will commence reading from it.  */
  8427.   {
  8428.     register FILE_BUF *ip2;
  8429.  
  8430.     ip2 = &instack[++indepth];
  8431.  
  8432.     ip2->fname = 0;
  8433.     ip2->nominal_fname = 0;
  8434.     /* This may not be exactly correct, but will give much better error
  8435.        messages for nested macro calls than using a line number of zero.  */
  8436.     ip2->lineno = start_line;
  8437.     ip2->buf = xbuf;
  8438.     ip2->length = xbuf_len;
  8439.     ip2->bufp = xbuf;
  8440.     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
  8441.     ip2->macro = hp;
  8442.     ip2->if_stack = if_stack;
  8443.     ip2->system_header_p = 0;
  8444.  
  8445.     /* Recursive macro use sometimes works traditionally.
  8446.        #define foo(x,y) bar (x (y,0), y)
  8447.        foo (foo, baz)  */
  8448.  
  8449.     if (!traditional)
  8450.       hp->type = T_DISABLED;
  8451.   }
  8452. }
  8453.  
  8454. /*
  8455.  * Parse a macro argument and store the info on it into *ARGPTR.
  8456.  * REST_ARGS is passed to macarg1 to make it absorb the rest of the args.
  8457.  * Return nonzero to indicate a syntax error.
  8458.  */
  8459.  
  8460. static char *
  8461. macarg (argptr, rest_args)
  8462.      register struct argdata *argptr;
  8463.      int rest_args;
  8464. {
  8465.   FILE_BUF *ip = &instack[indepth];
  8466.   int paren = 0;
  8467.   int newlines = 0;
  8468.   int comments = 0;
  8469.   char *result = 0;
  8470.  
  8471.   /* Try to parse as much of the argument as exists at this
  8472.      input stack level.  */
  8473.   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
  8474.             &paren, &newlines, &comments, rest_args);
  8475.  
  8476.   /* If we find the end of the argument at this level,
  8477.      set up *ARGPTR to point at it in the input stack.  */
  8478.   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
  8479.       && bp != ip->buf + ip->length) {
  8480.     if (argptr != 0) {
  8481.       argptr->raw = ip->bufp;
  8482.       argptr->raw_length = bp - ip->bufp;
  8483.       argptr->newlines = newlines;
  8484.     }
  8485.     ip->bufp = bp;
  8486.   } else {
  8487.     /* This input stack level ends before the macro argument does.
  8488.        We must pop levels and keep parsing.
  8489.        Therefore, we must allocate a temporary buffer and copy
  8490.        the macro argument into it.  */
  8491.     int bufsize = bp - ip->bufp;
  8492.     int extra = newlines;
  8493.     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
  8494.     int final_start = 0;
  8495.  
  8496.     bcopy ((char *) ip->bufp, (char *) buffer, bufsize);
  8497.     ip->bufp = bp;
  8498.     ip->lineno += newlines;
  8499.  
  8500.     while (bp == ip->buf + ip->length) {
  8501.       if (instack[indepth].macro == 0) {
  8502.     result = "unterminated macro call";
  8503.     break;
  8504.       }
  8505.       ip->macro->type = T_MACRO;
  8506.       if (ip->free_ptr)
  8507.     free (ip->free_ptr);
  8508.       ip = &instack[--indepth];
  8509.       newlines = 0;
  8510.       comments = 0;
  8511.       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
  8512.             &newlines, &comments, rest_args);
  8513.       final_start = bufsize;
  8514.       bufsize += bp - ip->bufp;
  8515.       extra += newlines;
  8516.       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
  8517.       bcopy ((char *) ip->bufp, (char *) (buffer + bufsize - (bp - ip->bufp)),
  8518.          bp - ip->bufp);
  8519.       ip->bufp = bp;
  8520.       ip->lineno += newlines;
  8521.     }
  8522.  
  8523.     /* Now, if arg is actually wanted, record its raw form,
  8524.        discarding comments and duplicating newlines in whatever
  8525.        part of it did not come from a macro expansion.
  8526.        EXTRA space has been preallocated for duplicating the newlines.
  8527.        FINAL_START is the index of the start of that part.  */
  8528.     if (argptr != 0) {
  8529.       argptr->raw = buffer;
  8530.       argptr->raw_length = bufsize;
  8531.       argptr->free1 = buffer;
  8532.       argptr->newlines = newlines;
  8533.       if ((newlines || comments) && ip->fname != 0)
  8534.     argptr->raw_length
  8535.       = final_start +
  8536.         discard_comments (argptr->raw + final_start,
  8537.                   argptr->raw_length - final_start,
  8538.                   newlines);
  8539.       argptr->raw[argptr->raw_length] = 0;
  8540.       if (argptr->raw_length > bufsize + extra)
  8541.     abort ();
  8542.     }
  8543.   }
  8544.  
  8545.   /* If we are not discarding this argument,
  8546.      macroexpand it and compute its length as stringified.
  8547.      All this info goes into *ARGPTR.  */
  8548.  
  8549.   if (argptr != 0) {
  8550.     register U_CHAR *buf, *lim;
  8551.     register int totlen;
  8552.  
  8553.     buf = argptr->raw;
  8554.     lim = buf + argptr->raw_length;
  8555.  
  8556.     while (buf != lim && is_space[*buf])
  8557.       buf++;
  8558.     while (buf != lim && is_space[lim[-1]])
  8559.       lim--;
  8560.     totlen = traditional ? 0 : 2;    /* Count opening and closing quote.  */
  8561.     while (buf != lim) {
  8562.       register U_CHAR c = *buf++;
  8563.       totlen++;
  8564.       /* Internal sequences of whitespace are replaced by one space
  8565.      in most cases, but not always.  So count all the whitespace
  8566.      in case we need to keep it all.  */
  8567. #if 0
  8568.       if (is_space[c])
  8569.     SKIP_ALL_WHITE_SPACE (buf);
  8570.       else
  8571. #endif
  8572.       if (c == '\"' || c == '\\') /* escape these chars */
  8573.     totlen++;
  8574.       else if (!isprint (c))
  8575.     totlen += 3;
  8576.     }
  8577.     argptr->stringified_length = totlen;
  8578.   }
  8579.   return result;
  8580. }
  8581.  
  8582. /* Scan text from START (inclusive) up to LIMIT (exclusive),
  8583.    counting parens in *DEPTHPTR,
  8584.    and return if reach LIMIT
  8585.    or before a `)' that would make *DEPTHPTR negative
  8586.    or before a comma when *DEPTHPTR is zero.
  8587.    Single and double quotes are matched and termination
  8588.    is inhibited within them.  Comments also inhibit it.
  8589.    Value returned is pointer to stopping place.
  8590.  
  8591.    Increment *NEWLINES each time a newline is passed.
  8592.    REST_ARGS notifies macarg1 that it should absorb the rest of the args.
  8593.    Set *COMMENTS to 1 if a comment is seen.  */
  8594.  
  8595. static U_CHAR *
  8596. macarg1 (start, limit, depthptr, newlines, comments, rest_args)
  8597.      U_CHAR *start;
  8598.      register U_CHAR *limit;
  8599.      int *depthptr, *newlines, *comments;
  8600.      int rest_args;
  8601. {
  8602.   register U_CHAR *bp = start;
  8603.  
  8604.   while (bp < limit) {
  8605.     switch (*bp) {
  8606.     case '(':
  8607.       (*depthptr)++;
  8608.       break;
  8609.     case ')':
  8610.       if (--(*depthptr) < 0)
  8611.     return bp;
  8612.       break;
  8613.     case '\\':
  8614.       /* Traditionally, backslash makes following char not special.  */
  8615.       if (bp + 1 < limit && traditional)
  8616.     {
  8617.       bp++;
  8618.       /* But count source lines anyway.  */
  8619.       if (*bp == '\n')
  8620.         ++*newlines;
  8621.     }
  8622.       break;
  8623.     case '\n':
  8624.       ++*newlines;
  8625.       break;
  8626.     case '/':
  8627.       if (bp[1] == '\\' && bp[2] == '\n')
  8628.     newline_fix (bp + 1);
  8629.       if (cplusplus_comments && bp[1] == '/') {
  8630.     *comments = 1;
  8631.     bp += 2;
  8632.     while (bp < limit && (*bp != '\n' || bp[-1] == '\\')) {
  8633.       if (*bp == '\n') ++*newlines;
  8634.       bp++;
  8635.     }
  8636.     /* Now count the newline that we are about to skip.  */
  8637.     ++*newlines;
  8638.     break;
  8639.       }
  8640.       if (bp[1] != '*' || bp + 1 >= limit)
  8641.     break;
  8642.       *comments = 1;
  8643.       bp += 2;
  8644.       while (bp + 1 < limit) {
  8645.     if (bp[0] == '*'
  8646.         && bp[1] == '\\' && bp[2] == '\n')
  8647.       newline_fix (bp + 1);
  8648.     if (bp[0] == '*' && bp[1] == '/')
  8649.       break;
  8650.     if (*bp == '\n') ++*newlines;
  8651.     bp++;
  8652.       }
  8653.       break;
  8654.     case '\'':
  8655.     case '\"':
  8656.       {
  8657.     int quotec;
  8658.     for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
  8659.       if (*bp == '\\') {
  8660.         bp++;
  8661.         if (*bp == '\n')
  8662.           ++*newlines;
  8663.         while (*bp == '\\' && bp[1] == '\n') {
  8664.           bp += 2;
  8665.         }
  8666.       } else if (*bp == '\n') {
  8667.         ++*newlines;
  8668.         if (quotec == '\'')
  8669.           break;
  8670.       }
  8671.     }
  8672.       }
  8673.       break;
  8674.     case ',':
  8675.       /* if we've returned to lowest level and we aren't absorbing all args */
  8676.       if ((*depthptr) == 0 && rest_args == 0)
  8677.     return bp;
  8678.       break;
  8679.     }
  8680.     bp++;
  8681.   }
  8682.  
  8683.   return bp;
  8684. }
  8685.  
  8686. /* Discard comments and duplicate newlines
  8687.    in the string of length LENGTH at START,
  8688.    except inside of string constants.
  8689.    The string is copied into itself with its beginning staying fixed.  
  8690.  
  8691.    NEWLINES is the number of newlines that must be duplicated.
  8692.    We assume that that much extra space is available past the end
  8693.    of the string.  */
  8694.  
  8695. static int
  8696. discard_comments (start, length, newlines)
  8697.      U_CHAR *start;
  8698.      int length;
  8699.      int newlines;
  8700. {
  8701.   register U_CHAR *ibp;
  8702.   register U_CHAR *obp;
  8703.   register U_CHAR *limit;
  8704.   register int c;
  8705.  
  8706.   /* If we have newlines to duplicate, copy everything
  8707.      that many characters up.  Then, in the second part,
  8708.      we will have room to insert the newlines
  8709.      while copying down.
  8710.      NEWLINES may actually be too large, because it counts
  8711.      newlines in string constants, and we don't duplicate those.
  8712.      But that does no harm.  */
  8713.   if (newlines > 0) {
  8714.     ibp = start + length;
  8715.     obp = ibp + newlines;
  8716.     limit = start;
  8717.     while (limit != ibp)
  8718.       *--obp = *--ibp;
  8719.   }
  8720.  
  8721.   ibp = start + newlines;
  8722.   limit = start + length + newlines;
  8723.   obp = start;
  8724.  
  8725.   while (ibp < limit) {
  8726.     *obp++ = c = *ibp++;
  8727.     switch (c) {
  8728.     case '\n':
  8729.       /* Duplicate the newline.  */
  8730.       *obp++ = '\n';
  8731.       break;
  8732.  
  8733.     case '\\':
  8734.       if (*ibp == '\n') {
  8735.     obp--;
  8736.     ibp++;
  8737.       }
  8738.       break;
  8739.  
  8740.     case '/':
  8741.       if (*ibp == '\\' && ibp[1] == '\n')
  8742.     newline_fix (ibp);
  8743.       /* Delete any comment.  */
  8744.       if (cplusplus_comments && ibp[0] == '/') {
  8745.     /* Comments are equivalent to spaces.  */
  8746.     obp[-1] = ' ';
  8747.     ibp++;
  8748.     while (ibp < limit && (*ibp != '\n' || ibp[-1] == '\\'))
  8749.       ibp++;
  8750.     break;
  8751.       }
  8752.       if (ibp[0] != '*' || ibp + 1 >= limit)
  8753.     break;
  8754.       /* Comments are equivalent to spaces.
  8755.      For -traditional, a comment is equivalent to nothing.  */
  8756.       if (traditional)
  8757.     obp--;
  8758.       else
  8759.     obp[-1] = ' ';
  8760.       ibp++;
  8761.       while (ibp + 1 < limit) {
  8762.     if (ibp[0] == '*'
  8763.         && ibp[1] == '\\' && ibp[2] == '\n')
  8764.       newline_fix (ibp + 1);
  8765.     if (ibp[0] == '*' && ibp[1] == '/')
  8766.       break;
  8767.     ibp++;
  8768.       }
  8769.       ibp += 2;
  8770.       break;
  8771.  
  8772.     case '\'':
  8773.     case '\"':
  8774.       /* Notice and skip strings, so that we don't
  8775.      think that comments start inside them,
  8776.      and so we don't duplicate newlines in them.  */
  8777.       {
  8778.     int quotec = c;
  8779.     while (ibp < limit) {
  8780.       *obp++ = c = *ibp++;
  8781.       if (c == quotec)
  8782.         break;
  8783.       if (c == '\n' && quotec == '\'')
  8784.         break;
  8785.       if (c == '\\' && ibp < limit) {
  8786.         while (*ibp == '\\' && ibp[1] == '\n')
  8787.           ibp += 2;
  8788.         *obp++ = *ibp++;
  8789.       }
  8790.     }
  8791.       }
  8792.       break;
  8793.     }
  8794.   }
  8795.  
  8796.   return obp - start;
  8797. }
  8798.  
  8799. /* Turn newlines to spaces in the string of length LENGTH at START,
  8800.    except inside of string constants.
  8801.    The string is copied into itself with its beginning staying fixed.  */
  8802.  
  8803. static int
  8804. change_newlines (start, length)
  8805.      U_CHAR *start;
  8806.      int length;
  8807. {
  8808.   register U_CHAR *ibp;
  8809.   register U_CHAR *obp;
  8810.   register U_CHAR *limit;
  8811.   register int c;
  8812.  
  8813.   ibp = start;
  8814.   limit = start + length;
  8815.   obp = start;
  8816.  
  8817.   while (ibp < limit) {
  8818.     *obp++ = c = *ibp++;
  8819.     switch (c) {
  8820.     case '\n':
  8821.       /* If this is a NEWLINE NEWLINE, then this is a real newline in the
  8822.      string.  Skip past the newline and its duplicate.
  8823.      Put a space in the output.  */
  8824.       if (*ibp == '\n')
  8825.     {
  8826.       ibp++;
  8827.       obp--;
  8828.       *obp++ = ' ';
  8829.     }
  8830.       break;
  8831.  
  8832.     case '\'':
  8833.     case '\"':
  8834.       /* Notice and skip strings, so that we don't delete newlines in them.  */
  8835.       {
  8836.     int quotec = c;
  8837.     while (ibp < limit) {
  8838.       *obp++ = c = *ibp++;
  8839.       if (c == quotec)
  8840.         break;
  8841.       if (c == '\n' && quotec == '\'')
  8842.         break;
  8843.     }
  8844.       }
  8845.       break;
  8846.     }
  8847.   }
  8848.  
  8849.   return obp - start;
  8850. }
  8851.  
  8852. /*
  8853.  * my_strerror - return the descriptive text associated with an `errno' code.
  8854.  */
  8855.  
  8856. char *
  8857. my_strerror (errnum)
  8858.      int errnum;
  8859. {
  8860.   char *result;
  8861.  
  8862. #ifndef VMS
  8863. #ifndef HAVE_STRERROR
  8864.   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
  8865. #else
  8866.   result = strerror (errnum);
  8867. #endif
  8868. #else    /* VMS */
  8869.   /* VAXCRTL's strerror() takes an optional second argument, which only
  8870.      matters when the first argument is EVMSERR.  However, it's simplest
  8871.      just to pass it unconditionally.  `vaxc$errno' is declared in
  8872.      <errno.h>, and maintained by the library in parallel with `errno'.
  8873.      We assume that caller's `errnum' either matches the last setting of
  8874.      `errno' by the library or else does not have the value `EVMSERR'.  */
  8875.  
  8876.   result = strerror (errnum, vaxc$errno);
  8877. #endif
  8878.  
  8879.   if (!result)
  8880.     result = "undocumented I/O error";
  8881.  
  8882.   return result;
  8883. }
  8884.  
  8885. /*
  8886.  * error - print error message and increment count of errors.
  8887.  */
  8888.  
  8889. void
  8890. error (PRINTF_ALIST (msg))
  8891.      PRINTF_DCL (msg)
  8892. {
  8893.   va_list args;
  8894.  
  8895.   VA_START (args, msg);
  8896.   verror (msg, args);
  8897.   va_end (args);
  8898. }
  8899.  
  8900. static void
  8901. verror (msg, args)
  8902.      char *msg;
  8903.      va_list args;
  8904. {
  8905.   int i;
  8906.   FILE_BUF *ip = NULL;
  8907.  
  8908.   print_containing_files ();
  8909.  
  8910.   for (i = indepth; i >= 0; i--)
  8911.     if (instack[i].fname != NULL) {
  8912.       ip = &instack[i];
  8913.       break;
  8914.     }
  8915.  
  8916.   if (ip != NULL)
  8917.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8918.   vfprintf (stderr, msg, args);
  8919.   fprintf (stderr, "\n");
  8920.   errors++;
  8921. }
  8922.  
  8923. /* Error including a message from `errno'.  */
  8924.  
  8925. static void
  8926. error_from_errno (name)
  8927.      char *name;
  8928. {
  8929.   int i;
  8930.   FILE_BUF *ip = NULL;
  8931.  
  8932.   print_containing_files ();
  8933.  
  8934.   for (i = indepth; i >= 0; i--)
  8935.     if (instack[i].fname != NULL) {
  8936.       ip = &instack[i];
  8937.       break;
  8938.     }
  8939.  
  8940.   if (ip != NULL)
  8941.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8942.  
  8943.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  8944.  
  8945.   errors++;
  8946. }
  8947.  
  8948. /* Print error message but don't count it.  */
  8949.  
  8950. void
  8951. warning (PRINTF_ALIST (msg))
  8952.      PRINTF_DCL (msg)
  8953. {
  8954.   va_list args;
  8955.  
  8956.   VA_START (args, msg);
  8957.   vwarning (msg, args);
  8958.   va_end (args);
  8959. }
  8960.  
  8961. static void
  8962. vwarning (msg, args)
  8963.      char *msg;
  8964.      va_list args;
  8965. {
  8966.   int i;
  8967.   FILE_BUF *ip = NULL;
  8968.  
  8969.   if (inhibit_warnings)
  8970.     return;
  8971.  
  8972.   if (warnings_are_errors)
  8973.     errors++;
  8974.  
  8975.   print_containing_files ();
  8976.  
  8977.   for (i = indepth; i >= 0; i--)
  8978.     if (instack[i].fname != NULL) {
  8979.       ip = &instack[i];
  8980.       break;
  8981.     }
  8982.  
  8983.   if (ip != NULL)
  8984.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, ip->lineno);
  8985.   fprintf (stderr, "warning: ");
  8986.   vfprintf (stderr, msg, args);
  8987.   fprintf (stderr, "\n");
  8988. }
  8989.  
  8990. static void
  8991. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  8992. error_with_line (int line, PRINTF_ALIST (msg))
  8993. #else
  8994. error_with_line (line, PRINTF_ALIST (msg))
  8995.      int line;
  8996.      PRINTF_DCL (msg)
  8997. #endif
  8998. {
  8999.   va_list args;
  9000.  
  9001.   VA_START (args, msg);
  9002.   verror_with_line (line, msg, args);
  9003.   va_end (args);
  9004. }
  9005.  
  9006. static void
  9007. verror_with_line (line, msg, args)
  9008.      int line;
  9009.      char *msg;
  9010.      va_list args;
  9011. {
  9012.   int i;
  9013.   FILE_BUF *ip = NULL;
  9014.  
  9015.   print_containing_files ();
  9016.  
  9017.   for (i = indepth; i >= 0; i--)
  9018.     if (instack[i].fname != NULL) {
  9019.       ip = &instack[i];
  9020.       break;
  9021.     }
  9022.  
  9023.   if (ip != NULL)
  9024.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  9025.   vfprintf (stderr, msg, args);
  9026.   fprintf (stderr, "\n");
  9027.   errors++;
  9028. }
  9029.  
  9030. static void
  9031. vwarning_with_line (line, msg, args)
  9032.      int line;
  9033.      char *msg;
  9034.      va_list args;
  9035. {
  9036.   int i;
  9037.   FILE_BUF *ip = NULL;
  9038.  
  9039.   if (inhibit_warnings)
  9040.     return;
  9041.  
  9042.   if (warnings_are_errors)
  9043.     errors++;
  9044.  
  9045.   print_containing_files ();
  9046.  
  9047.   for (i = indepth; i >= 0; i--)
  9048.     if (instack[i].fname != NULL) {
  9049.       ip = &instack[i];
  9050.       break;
  9051.     }
  9052.  
  9053.   if (ip != NULL)
  9054.     fprintf (stderr, "%s:%d: ", ip->nominal_fname, line);
  9055.   fprintf (stderr, "warning: ");
  9056.   vfprintf (stderr, msg, args);
  9057.   fprintf (stderr, "\n");
  9058. }
  9059.  
  9060. /* print an error message and maybe count it.  */
  9061.  
  9062. void
  9063. pedwarn (PRINTF_ALIST (msg))
  9064.      PRINTF_DCL (msg)
  9065. {
  9066.   va_list args;
  9067.  
  9068.   VA_START (args, msg);
  9069.   if (pedantic_errors)
  9070.     verror (msg, args);
  9071.   else
  9072.     vwarning (msg, args);
  9073.   va_end (args);
  9074. }
  9075.  
  9076. void
  9077. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9078. pedwarn_with_line (int line, PRINTF_ALIST (msg))
  9079. #else
  9080. pedwarn_with_line (line, PRINTF_ALIST (msg))
  9081.      int line;
  9082.      PRINTF_DCL (msg)
  9083. #endif
  9084. {
  9085.   va_list args;
  9086.  
  9087.   VA_START (args, msg);
  9088.   if (pedantic_errors)
  9089.     verror_with_line (line, msg, args);
  9090.   else
  9091.     vwarning_with_line (line, msg, args);
  9092.   va_end (args);
  9093. }
  9094.  
  9095. /* Report a warning (or an error if pedantic_errors)
  9096.    giving specified file name and line number, not current.  */
  9097.  
  9098. static void
  9099. #if defined (__STDC__) && defined (HAVE_VPRINTF)
  9100. pedwarn_with_file_and_line (char *file, int line, PRINTF_ALIST (msg))
  9101. #else
  9102. pedwarn_with_file_and_line (file, line, PRINTF_ALIST (msg))
  9103.      char *file;
  9104.      int line;
  9105.      PRINTF_DCL (msg)
  9106. #endif
  9107. {
  9108.   va_list args;
  9109.  
  9110.   if (!pedantic_errors && inhibit_warnings)
  9111.     return;
  9112.   if (file != NULL)
  9113.     fprintf (stderr, "%s:%d: ", file, line);
  9114.   if (pedantic_errors)
  9115.     errors++;
  9116.   if (!pedantic_errors)
  9117.     fprintf (stderr, "warning: ");
  9118.   VA_START (args, msg);
  9119.   vfprintf (stderr, msg, args);
  9120.   va_end (args);
  9121.   fprintf (stderr, "\n");
  9122. }
  9123.  
  9124. /* Print the file names and line numbers of the #include
  9125.    directives which led to the current file.  */
  9126.  
  9127. static void
  9128. print_containing_files ()
  9129. {
  9130.   FILE_BUF *ip = NULL;
  9131.   int i;
  9132.   int first = 1;
  9133.  
  9134.   /* If stack of files hasn't changed since we last printed
  9135.      this info, don't repeat it.  */
  9136.   if (last_error_tick == input_file_stack_tick)
  9137.     return;
  9138.  
  9139.   for (i = indepth; i >= 0; i--)
  9140.     if (instack[i].fname != NULL) {
  9141.       ip = &instack[i];
  9142.       break;
  9143.     }
  9144.  
  9145.   /* Give up if we don't find a source file.  */
  9146.   if (ip == NULL)
  9147.     return;
  9148.  
  9149.   /* Find the other, outer source files.  */
  9150.   for (i--; i >= 0; i--)
  9151.     if (instack[i].fname != NULL) {
  9152.       ip = &instack[i];
  9153.       if (first) {
  9154.     first = 0;
  9155.     fprintf (stderr, "In file included");
  9156.       } else {
  9157.     fprintf (stderr, ",\n                ");
  9158.       }
  9159.  
  9160.       fprintf (stderr, " from %s:%d", ip->nominal_fname, ip->lineno);
  9161.     }
  9162.   if (! first)
  9163.     fprintf (stderr, ":\n");
  9164.  
  9165.   /* Record we have printed the status as of this time.  */
  9166.   last_error_tick = input_file_stack_tick;
  9167. }
  9168.  
  9169. /* Return the line at which an error occurred.
  9170.    The error is not necessarily associated with the current spot
  9171.    in the input stack, so LINE says where.  LINE will have been
  9172.    copied from ip->lineno for the current input level.
  9173.    If the current level is for a file, we return LINE.
  9174.    But if the current level is not for a file, LINE is meaningless.
  9175.    In that case, we return the lineno of the innermost file.  */
  9176.  
  9177. static int
  9178. line_for_error (line)
  9179.      int line;
  9180. {
  9181.   int i;
  9182.   int line1 = line;
  9183.  
  9184.   for (i = indepth; i >= 0; ) {
  9185.     if (instack[i].fname != 0)
  9186.       return line1;
  9187.     i--;
  9188.     if (i < 0)
  9189.       return 0;
  9190.     line1 = instack[i].lineno;
  9191.   }
  9192.   abort ();
  9193.   /*NOTREACHED*/
  9194.   return 0;
  9195. }
  9196.  
  9197. /*
  9198.  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
  9199.  *
  9200.  * As things stand, nothing is ever placed in the output buffer to be
  9201.  * removed again except when it's KNOWN to be part of an identifier,
  9202.  * so flushing and moving down everything left, instead of expanding,
  9203.  * should work ok.
  9204.  */
  9205.  
  9206. /* You might think void was cleaner for the return type,
  9207.    but that would get type mismatch in check_expand in strict ANSI.  */
  9208. static int
  9209. grow_outbuf (obuf, needed)
  9210.      register FILE_BUF *obuf;
  9211.      register int needed;
  9212. {
  9213.   register U_CHAR *p;
  9214.   int minsize;
  9215.  
  9216.   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
  9217.     return 0;
  9218.  
  9219.   /* Make it at least twice as big as it is now.  */
  9220.   obuf->length *= 2;
  9221.   /* Make it have at least 150% of the free space we will need.  */
  9222.   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
  9223.   if (minsize > obuf->length)
  9224.     obuf->length = minsize;
  9225.  
  9226.   if ((p = (U_CHAR *) xrealloc (obuf->buf, obuf->length)) == NULL)
  9227.     memory_full ();
  9228.  
  9229.   obuf->bufp = p + (obuf->bufp - obuf->buf);
  9230.   obuf->buf = p;
  9231.  
  9232.   return 0;
  9233. }
  9234.  
  9235. /* Symbol table for macro names and special symbols */
  9236.  
  9237. /*
  9238.  * install a name in the main hash table, even if it is already there.
  9239.  *   name stops with first non alphanumeric, except leading '#'.
  9240.  * caller must check against redefinition if that is desired.
  9241.  * delete_macro () removes things installed by install () in fifo order.
  9242.  * this is important because of the `defined' special symbol used
  9243.  * in #if, and also if pushdef/popdef directives are ever implemented.
  9244.  *
  9245.  * If LEN is >= 0, it is the length of the name.
  9246.  * Otherwise, compute the length by scanning the entire name.
  9247.  *
  9248.  * If HASH is >= 0, it is the precomputed hash code.
  9249.  * Otherwise, compute the hash code.
  9250.  */
  9251. static HASHNODE *
  9252. install (name, len, type, value, hash)
  9253.      U_CHAR *name;
  9254.      int len;
  9255.      enum node_type type;
  9256.      char *value;
  9257.      int hash;
  9258. {
  9259.   register HASHNODE *hp;
  9260.   register int i, bucket;
  9261.   register U_CHAR *p, *q;
  9262.  
  9263.   if (len < 0) {
  9264.     p = name;
  9265.     while (is_idchar[*p])
  9266.       p++;
  9267.     len = p - name;
  9268.   }
  9269.  
  9270.   if (hash < 0)
  9271.     hash = hashf (name, len, HASHSIZE);
  9272.  
  9273.   i = sizeof (HASHNODE) + len + 1;
  9274.   hp = (HASHNODE *) xmalloc (i);
  9275.   bucket = hash;
  9276.   hp->bucket_hdr = &hashtab[bucket];
  9277.   hp->next = hashtab[bucket];
  9278.   hashtab[bucket] = hp;
  9279.   hp->prev = NULL;
  9280.   if (hp->next != NULL)
  9281.     hp->next->prev = hp;
  9282.   hp->type = type;
  9283.   hp->length = len;
  9284.   hp->value.cpval = value;
  9285.   hp->name = ((U_CHAR *) hp) + sizeof (HASHNODE);
  9286.   p = hp->name;
  9287.   q = name;
  9288.   for (i = 0; i < len; i++)
  9289.     *p++ = *q++;
  9290.   hp->name[len] = 0;
  9291.   return hp;
  9292. }
  9293.  
  9294. /*
  9295.  * find the most recent hash node for name name (ending with first
  9296.  * non-identifier char) installed by install
  9297.  *
  9298.  * If LEN is >= 0, it is the length of the name.
  9299.  * Otherwise, compute the length by scanning the entire name.
  9300.  *
  9301.  * If HASH is >= 0, it is the precomputed hash code.
  9302.  * Otherwise, compute the hash code.
  9303.  */
  9304. HASHNODE *
  9305. lookup (name, len, hash)
  9306.      U_CHAR *name;
  9307.      int len;
  9308.      int hash;
  9309. {
  9310.   register U_CHAR *bp;
  9311.   register HASHNODE *bucket;
  9312.  
  9313.   if (len < 0) {
  9314.     for (bp = name; is_idchar[*bp]; bp++) ;
  9315.     len = bp - name;
  9316.   }
  9317.  
  9318.   if (hash < 0)
  9319.     hash = hashf (name, len, HASHSIZE);
  9320.  
  9321.   bucket = hashtab[hash];
  9322.   while (bucket) {
  9323.     if (bucket->length == len && bcmp (bucket->name, name, len) == 0)
  9324.       return bucket;
  9325.     bucket = bucket->next;
  9326.   }
  9327.   return NULL;
  9328. }
  9329.  
  9330. /*
  9331.  * Delete a hash node.  Some weirdness to free junk from macros.
  9332.  * More such weirdness will have to be added if you define more hash
  9333.  * types that need it.
  9334.  */
  9335.  
  9336. /* Note that the DEFINITION of a macro is removed from the hash table
  9337.    but its storage is not freed.  This would be a storage leak
  9338.    except that it is not reasonable to keep undefining and redefining
  9339.    large numbers of macros many times.
  9340.    In any case, this is necessary, because a macro can be #undef'd
  9341.    in the middle of reading the arguments to a call to it.
  9342.    If #undef freed the DEFINITION, that would crash.  */
  9343.  
  9344. static void
  9345. delete_macro (hp)
  9346.      HASHNODE *hp;
  9347. {
  9348.  
  9349.   if (hp->prev != NULL)
  9350.     hp->prev->next = hp->next;
  9351.   if (hp->next != NULL)
  9352.     hp->next->prev = hp->prev;
  9353.  
  9354.   /* make sure that the bucket chain header that
  9355.      the deleted guy was on points to the right thing afterwards. */
  9356.   if (hp == *hp->bucket_hdr)
  9357.     *hp->bucket_hdr = hp->next;
  9358.  
  9359. #if 0
  9360.   if (hp->type == T_MACRO) {
  9361.     DEFINITION *d = hp->value.defn;
  9362.     struct reflist *ap, *nextap;
  9363.  
  9364.     for (ap = d->pattern; ap != NULL; ap = nextap) {
  9365.       nextap = ap->next;
  9366.       free (ap);
  9367.     }
  9368.     free (d);
  9369.   }
  9370. #endif
  9371.   free (hp);
  9372. }
  9373.  
  9374. /*
  9375.  * return hash function on name.  must be compatible with the one
  9376.  * computed a step at a time, elsewhere
  9377.  */
  9378. static int
  9379. hashf (name, len, hashsize)
  9380.      register U_CHAR *name;
  9381.      register int len;
  9382.      int hashsize;
  9383. {
  9384.   register int r = 0;
  9385.  
  9386.   while (len--)
  9387.     r = HASHSTEP (r, *name++);
  9388.  
  9389.   return MAKE_POS (r) % hashsize;
  9390. }
  9391.  
  9392.  
  9393. /* Dump the definition of a single macro HP to OF.  */
  9394. static void
  9395. dump_single_macro (hp, of)
  9396.      register HASHNODE *hp;
  9397.      FILE *of;
  9398. {
  9399.   register DEFINITION *defn = hp->value.defn;
  9400.   struct reflist *ap;
  9401.   int offset;
  9402.   int concat;
  9403.  
  9404.  
  9405.   /* Print the definition of the macro HP.  */
  9406.  
  9407.   fprintf (of, "#define %s", hp->name);
  9408.  
  9409.   if (defn->nargs >= 0) {
  9410.     int i;
  9411.  
  9412.     fprintf (of, "(");
  9413.     for (i = 0; i < defn->nargs; i++) {
  9414.       dump_arg_n (defn, i, of);
  9415.       if (i + 1 < defn->nargs)
  9416.     fprintf (of, ", ");
  9417.     }
  9418.     fprintf (of, ")");
  9419.   }
  9420.  
  9421.   fprintf (of, " ");
  9422.  
  9423.   offset = 0;
  9424.   concat = 0;
  9425.   for (ap = defn->pattern; ap != NULL; ap = ap->next) {
  9426.     dump_defn_1 (defn->expansion, offset, ap->nchars, of);
  9427.     offset += ap->nchars;
  9428.     if (!traditional) {
  9429.       if (ap->nchars != 0)
  9430.     concat = 0;
  9431.       if (ap->stringify) {
  9432.     switch (ap->stringify) {
  9433.      case SHARP_TOKEN: fprintf (of, "#"); break;
  9434.      case WHITE_SHARP_TOKEN: fprintf (of, "# "); break;
  9435.      case PERCENT_COLON_TOKEN: fprintf (of, "%%:"); break;
  9436.      case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%: "); break;
  9437.      default: abort ();
  9438.     }
  9439.       }
  9440.       if (ap->raw_before != 0) {
  9441.     if (concat) {
  9442.       switch (ap->raw_before) {
  9443.        case WHITE_SHARP_TOKEN:
  9444.        case WHITE_PERCENT_COLON_TOKEN:
  9445.         fprintf (of, " ");
  9446.         break;
  9447.        default:
  9448.         break;
  9449.       }
  9450.     } else {
  9451.       switch (ap->raw_before) {
  9452.        case SHARP_TOKEN: fprintf (of, "##"); break;
  9453.        case WHITE_SHARP_TOKEN: fprintf (of, "## "); break;
  9454.        case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
  9455.        case WHITE_PERCENT_COLON_TOKEN: fprintf (of, "%%:%%: "); break;
  9456.        default: abort ();
  9457.       }
  9458.     }
  9459.       }
  9460.       concat = 0;
  9461.     }
  9462.     dump_arg_n (defn, ap->argno, of);
  9463.     if (!traditional && ap->raw_after != 0) {
  9464.       switch (ap->raw_after) {
  9465.        case SHARP_TOKEN: fprintf (of, "##"); break;
  9466.        case WHITE_SHARP_TOKEN: fprintf (of, " ##"); break;
  9467.        case PERCENT_COLON_TOKEN: fprintf (of, "%%:%%:"); break;
  9468.        case WHITE_PERCENT_COLON_TOKEN: fprintf (of, " %%:%%:"); break;
  9469.        default: abort ();
  9470.       }
  9471.       concat = 1;
  9472.     }
  9473.   }
  9474.   dump_defn_1 (defn->expansion, offset, defn->length - offset, of);
  9475.   fprintf (of, "\n");
  9476. }
  9477.  
  9478. /* Dump all macro definitions as #defines to stdout.  */
  9479.  
  9480. static void
  9481. dump_all_macros ()
  9482. {
  9483.   int bucket;
  9484.  
  9485.   for (bucket = 0; bucket < HASHSIZE; bucket++) {
  9486.     register HASHNODE *hp;
  9487.  
  9488.     for (hp = hashtab[bucket]; hp; hp= hp->next) {
  9489.       if (hp->type == T_MACRO)
  9490.     dump_single_macro (hp, stdout);
  9491.     }
  9492.   }
  9493. }
  9494.  
  9495. /* Output to OF a substring of a macro definition.
  9496.    BASE is the beginning of the definition.
  9497.    Output characters START thru LENGTH.
  9498.    Unless traditional, discard newlines outside of strings, thus
  9499.    converting funny-space markers to ordinary spaces.  */
  9500.  
  9501. static void
  9502. dump_defn_1 (base, start, length, of)
  9503.      U_CHAR *base;
  9504.      int start;
  9505.      int length;
  9506.      FILE *of;
  9507. {
  9508.   U_CHAR *p = base + start;
  9509.   U_CHAR *limit = base + start + length;
  9510.  
  9511.   if (traditional)
  9512.     fwrite (p, sizeof (*p), length, of);
  9513.   else {
  9514.     while (p < limit) {
  9515.       if (*p == '\"' || *p =='\'') {
  9516.     U_CHAR *p1 = skip_quoted_string (p, limit, 0, NULL_PTR,
  9517.                      NULL_PTR, NULL_PTR);
  9518.     fwrite (p, sizeof (*p), p1 - p, of);
  9519.     p = p1;
  9520.       } else {
  9521.     if (*p != '\n')
  9522.       putc (*p, of);
  9523.     p++;
  9524.       }
  9525.     }
  9526.   }
  9527. }
  9528.  
  9529. /* Print the name of argument number ARGNUM of macro definition DEFN
  9530.    to OF.
  9531.    Recall that DEFN->args.argnames contains all the arg names
  9532.    concatenated in reverse order with comma-space in between.  */
  9533.  
  9534. static void
  9535. dump_arg_n (defn, argnum, of)
  9536.      DEFINITION *defn;
  9537.      int argnum;
  9538.      FILE *of;
  9539. {
  9540.   register U_CHAR *p = defn->args.argnames;
  9541.   while (argnum + 1 < defn->nargs) {
  9542.     p = (U_CHAR *) index ((char *) p, ' ') + 1;
  9543.     argnum++;
  9544.   }
  9545.  
  9546.   while (*p && *p != ',') {
  9547.     putc (*p, of);
  9548.     p++;
  9549.   }
  9550. }
  9551.  
  9552. /* Initialize syntactic classifications of characters.  */
  9553.  
  9554. static void
  9555. initialize_char_syntax ()
  9556. {
  9557.   register int i;
  9558.  
  9559.   /*
  9560.    * Set up is_idchar and is_idstart tables.  These should be
  9561.    * faster than saying (is_alpha (c) || c == '_'), etc.
  9562.    * Set up these things before calling any routines tthat
  9563.    * refer to them.
  9564.    */
  9565.   for (i = 'a'; i <= 'z'; i++) {
  9566.     is_idchar[i - 'a' + 'A'] = 1;
  9567.     is_idchar[i] = 1;
  9568.     is_idstart[i - 'a' + 'A'] = 1;
  9569.     is_idstart[i] = 1;
  9570.   }
  9571.   for (i = '0'; i <= '9'; i++)
  9572.     is_idchar[i] = 1;
  9573.   is_idchar['_'] = 1;
  9574.   is_idstart['_'] = 1;
  9575.   is_idchar['$'] = dollars_in_ident;
  9576.   is_idstart['$'] = dollars_in_ident;
  9577.  
  9578.   /* horizontal space table */
  9579.   is_hor_space[' '] = 1;
  9580.   is_hor_space['\t'] = 1;
  9581.   is_hor_space['\v'] = 1;
  9582.   is_hor_space['\f'] = 1;
  9583.   is_hor_space['\r'] = 1;
  9584.  
  9585.   is_space[' '] = 1;
  9586.   is_space['\t'] = 1;
  9587.   is_space['\v'] = 1;
  9588.   is_space['\f'] = 1;
  9589.   is_space['\n'] = 1;
  9590.   is_space['\r'] = 1;
  9591.  
  9592.   char_name['\v'] = "vertical tab";
  9593.   char_name['\f'] = "formfeed";
  9594.   char_name['\r'] = "carriage return";
  9595. }
  9596.  
  9597. /* Initialize the built-in macros.  */
  9598.  
  9599. static void
  9600. initialize_builtins (inp, outp)
  9601.      FILE_BUF *inp;
  9602.      FILE_BUF *outp;
  9603. {
  9604.   install ((U_CHAR *) "__LINE__", -1, T_SPECLINE, NULL_PTR, -1);
  9605.   install ((U_CHAR *) "__DATE__", -1, T_DATE, NULL_PTR, -1);
  9606.   install ((U_CHAR *) "__FILE__", -1, T_FILE, NULL_PTR, -1);
  9607.   install ((U_CHAR *) "__BASE_FILE__", -1, T_BASE_FILE, NULL_PTR, -1);
  9608.   install ((U_CHAR *) "__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, NULL_PTR, -1);
  9609.   install ((U_CHAR *) "__VERSION__", -1, T_VERSION, NULL_PTR, -1);
  9610. #ifndef NO_BUILTIN_SIZE_TYPE
  9611.   install ((U_CHAR *) "__SIZE_TYPE__", -1, T_SIZE_TYPE, NULL_PTR, -1);
  9612. #endif
  9613. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9614.   install ((U_CHAR *) "__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, NULL_PTR, -1);
  9615. #endif
  9616.   install ((U_CHAR *) "__WCHAR_TYPE__", -1, T_WCHAR_TYPE, NULL_PTR, -1);
  9617.   install ((U_CHAR *) "__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE,
  9618.        NULL_PTR, -1);
  9619.   install ((U_CHAR *) "__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE,
  9620.        NULL_PTR, -1);
  9621.   install ((U_CHAR *) "__IMMEDIATE_PREFIX__", -1, T_IMMEDIATE_PREFIX_TYPE,
  9622.        NULL_PTR, -1);
  9623.   install ((U_CHAR *) "__TIME__", -1, T_TIME, NULL_PTR, -1);
  9624.   if (!traditional) {
  9625.     install ((U_CHAR *) "__STDC__", -1, T_CONST, "1", -1);
  9626.     install ((U_CHAR *) "__STDC_VERSION__", -1, T_CONST, "199409L", -1);
  9627.   }
  9628.   if (objc)
  9629.     install ((U_CHAR *) "__OBJC__", -1, T_CONST, "1", -1);
  9630. /*  This is supplied using a -D by the compiler driver
  9631.     so that it is present only when truly compiling with GNU C.  */
  9632. /*  install ((U_CHAR *) "__GNUC__", -1, T_CONST, "2", -1);  */
  9633.  
  9634.   if (debug_output)
  9635.     {
  9636.       char directive[2048];
  9637.       U_CHAR *udirective = (U_CHAR *) directive;
  9638.       register struct directive *dp = &directive_table[0];
  9639.       struct tm *timebuf = timestamp ();
  9640.  
  9641.       sprintf (directive, " __BASE_FILE__ \"%s\"\n",
  9642.            instack[0].nominal_fname);
  9643.       output_line_directive (inp, outp, 0, same_file);
  9644.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9645.                outp, dp);
  9646.  
  9647.       sprintf (directive, " __VERSION__ \"%s\"\n", version_string);
  9648.       output_line_directive (inp, outp, 0, same_file);
  9649.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9650.                outp, dp);
  9651.  
  9652. #ifndef NO_BUILTIN_SIZE_TYPE
  9653.       sprintf (directive, " __SIZE_TYPE__ %s\n", SIZE_TYPE);
  9654.       output_line_directive (inp, outp, 0, same_file);
  9655.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9656.                outp, dp);
  9657. #endif
  9658.  
  9659. #ifndef NO_BUILTIN_PTRDIFF_TYPE
  9660.       sprintf (directive, " __PTRDIFF_TYPE__ %s\n", PTRDIFF_TYPE);
  9661.       output_line_directive (inp, outp, 0, same_file);
  9662.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9663.                outp, dp);
  9664. #endif
  9665.  
  9666.       sprintf (directive, " __WCHAR_TYPE__ %s\n", wchar_type);
  9667.       output_line_directive (inp, outp, 0, same_file);
  9668.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9669.                outp, dp);
  9670.  
  9671.       sprintf (directive, " __DATE__ \"%s %2d %4d\"\n",
  9672.            monthnames[timebuf->tm_mon],
  9673.            timebuf->tm_mday, timebuf->tm_year + 1900);
  9674.       output_line_directive (inp, outp, 0, same_file);
  9675.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9676.                outp, dp);
  9677.  
  9678.       sprintf (directive, " __TIME__ \"%02d:%02d:%02d\"\n",
  9679.            timebuf->tm_hour, timebuf->tm_min, timebuf->tm_sec);
  9680.       output_line_directive (inp, outp, 0, same_file);
  9681.       pass_thru_directive (udirective, &udirective[strlen (directive)],
  9682.                outp, dp);
  9683.  
  9684.       if (!traditional)
  9685.     {
  9686.           sprintf (directive, " __STDC__ 1");
  9687.           output_line_directive (inp, outp, 0, same_file);
  9688.           pass_thru_directive (udirective, &udirective[strlen (directive)],
  9689.                    outp, dp);
  9690.     }
  9691.       if (objc)
  9692.     {
  9693.           sprintf (directive, " __OBJC__ 1");
  9694.           output_line_directive (inp, outp, 0, same_file);
  9695.           pass_thru_directive (udirective, &udirective[strlen (directive)],
  9696.                    outp, dp);
  9697.     }
  9698.     }
  9699. }
  9700.  
  9701. /*
  9702.  * process a given definition string, for initialization
  9703.  * If STR is just an identifier, define it with value 1.
  9704.  * If STR has anything after the identifier, then it should
  9705.  * be identifier=definition.
  9706.  */
  9707.  
  9708. static void
  9709. make_definition (str, op)
  9710.      char *str;
  9711.      FILE_BUF *op;
  9712. {
  9713.   FILE_BUF *ip;
  9714.   struct directive *kt;
  9715.   U_CHAR *buf, *p;
  9716.  
  9717.   p = buf = (U_CHAR *) str;
  9718.   if (!is_idstart[*p]) {
  9719.     error ("malformed option `-D %s'", str);
  9720.     return;
  9721.   }
  9722.   while (is_idchar[*++p])
  9723.     ;
  9724.   if (*p == '(') {
  9725.     while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
  9726.       ;
  9727.     if (*p++ != ')')
  9728.       p = (U_CHAR *) str;            /* Error */
  9729.   }
  9730.   if (*p == 0) {
  9731.     buf = (U_CHAR *) alloca (p - buf + 4);
  9732.     strcpy ((char *)buf, str);
  9733.     strcat ((char *)buf, " 1");
  9734.   } else if (*p != '=') {
  9735.     error ("malformed option `-D %s'", str);
  9736.     return;
  9737.   } else {
  9738.     U_CHAR *q;
  9739.     /* Copy the entire option so we can modify it.  */
  9740.     buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
  9741.     strncpy ((char *) buf, str, p - (U_CHAR *) str);
  9742.     /* Change the = to a space.  */
  9743.     buf[p - (U_CHAR *) str] = ' ';
  9744.     /* Scan for any backslash-newline and remove it.  */
  9745.     p++;
  9746.     q = &buf[p - (U_CHAR *) str];
  9747.     while (*p) {
  9748.       if (*p == '\"' || *p == '\'') {
  9749.     int unterminated = 0;
  9750.     U_CHAR *p1 = skip_quoted_string (p, p + strlen ((char *) p), 0,
  9751.                      NULL_PTR, NULL_PTR, &unterminated);
  9752.     if (unterminated)
  9753.       return;
  9754.     while (p != p1)
  9755.       if (*p == '\\' && p[1] == '\n')
  9756.         p += 2;
  9757.       else
  9758.         *q++ = *p++;
  9759.       } else if (*p == '\\' && p[1] == '\n')
  9760.     p += 2;
  9761.       /* Change newline chars into newline-markers.  */
  9762.       else if (*p == '\n')
  9763.     {
  9764.       *q++ = '\n';
  9765.       *q++ = '\n';
  9766.       p++;
  9767.     }
  9768.       else
  9769.     *q++ = *p++;
  9770.     }
  9771.     *q = 0;
  9772.   }
  9773.   
  9774.   ip = &instack[++indepth];
  9775.   ip->nominal_fname = ip->fname = "*Initialization*";
  9776.  
  9777.   ip->buf = ip->bufp = buf;
  9778.   ip->length = strlen ((char *) buf);
  9779.   ip->lineno = 1;
  9780.   ip->macro = 0;
  9781.   ip->free_ptr = 0;
  9782.   ip->if_stack = if_stack;
  9783.   ip->system_header_p = 0;
  9784.  
  9785.   for (kt = directive_table; kt->type != T_DEFINE; kt++)
  9786.     ;
  9787.  
  9788.   /* Pass NULL instead of OP, since this is a "predefined" macro.  */
  9789.   do_define (buf, buf + strlen ((char *) buf), NULL_PTR, kt);
  9790.   --indepth;
  9791. }
  9792.  
  9793. /* JF, this does the work for the -U option */
  9794.  
  9795. static void
  9796. make_undef (str, op)
  9797.      char *str;
  9798.      FILE_BUF *op;
  9799. {
  9800.   FILE_BUF *ip;
  9801.   struct directive *kt;
  9802.  
  9803.   ip = &instack[++indepth];
  9804.   ip->nominal_fname = ip->fname = "*undef*";
  9805.  
  9806.   ip->buf = ip->bufp = (U_CHAR *) str;
  9807.   ip->length = strlen (str);
  9808.   ip->lineno = 1;
  9809.   ip->macro = 0;
  9810.   ip->free_ptr = 0;
  9811.   ip->if_stack = if_stack;
  9812.   ip->system_header_p = 0;
  9813.  
  9814.   for (kt = directive_table; kt->type != T_UNDEF; kt++)
  9815.     ;
  9816.  
  9817.   do_undef ((U_CHAR *) str, (U_CHAR *) str + strlen (str), op, kt);
  9818.   --indepth;
  9819. }
  9820.  
  9821. /* Process the string STR as if it appeared as the body of a #assert.
  9822.    OPTION is the option name for which STR was the argument.  */
  9823.  
  9824. static void
  9825. make_assertion (option, str)
  9826.      char *option;
  9827.      char *str;
  9828. {
  9829.   FILE_BUF *ip;
  9830.   struct directive *kt;
  9831.   U_CHAR *buf, *p, *q;
  9832.  
  9833.   /* Copy the entire option so we can modify it.  */
  9834.   buf = (U_CHAR *) alloca (strlen (str) + 1);
  9835.   strcpy ((char *) buf, str);
  9836.   /* Scan for any backslash-newline and remove it.  */
  9837.   p = q = buf;
  9838.   while (*p) {
  9839.     if (*p == '\\' && p[1] == '\n')
  9840.       p += 2;
  9841.     else
  9842.       *q++ = *p++;
  9843.   }
  9844.   *q = 0;
  9845.  
  9846.   p = buf;
  9847.   if (!is_idstart[*p]) {
  9848.     error ("malformed option `%s %s'", option, str);
  9849.     return;
  9850.   }
  9851.   while (is_idchar[*++p])
  9852.     ;
  9853.   SKIP_WHITE_SPACE (p);
  9854.   if (! (*p == 0 || *p == '(')) {
  9855.     error ("malformed option `%s %s'", option, str);
  9856.     return;
  9857.   }
  9858.   
  9859.   ip = &instack[++indepth];
  9860.   ip->nominal_fname = ip->fname = "*Initialization*";
  9861.  
  9862.   ip->buf = ip->bufp = buf;
  9863.   ip->length = strlen ((char *) buf);
  9864.   ip->lineno = 1;
  9865.   ip->macro = 0;
  9866.   ip->free_ptr = 0;
  9867.   ip->if_stack = if_stack;
  9868.   ip->system_header_p = 0;
  9869.  
  9870.   for (kt = directive_table; kt->type != T_ASSERT; kt++)
  9871.     ;
  9872.  
  9873.   /* pass NULL as output ptr to do_define since we KNOW it never
  9874.      does any output.... */
  9875.   do_assert (buf, buf + strlen ((char *) buf) , NULL_PTR, kt);
  9876.   --indepth;
  9877. }
  9878.  
  9879. /* Append a chain of `struct file_name_list's
  9880.    to the end of the main include chain.
  9881.    FIRST is the beginning of the chain to append, and LAST is the end.  */
  9882.  
  9883. static void
  9884. append_include_chain (first, last)
  9885.      struct file_name_list *first, *last;
  9886. {
  9887.   struct file_name_list *dir;
  9888.  
  9889.   if (!first || !last)
  9890.     return;
  9891.  
  9892.   if (include == 0)
  9893.     include = first;
  9894.   else
  9895.     last_include->next = first;
  9896.  
  9897.   if (first_bracket_include == 0)
  9898.     first_bracket_include = first;
  9899.  
  9900.   for (dir = first; ; dir = dir->next) {
  9901.     int len = strlen (dir->fname) + INCLUDE_LEN_FUDGE;
  9902.     if (len > max_include_len)
  9903.       max_include_len = len;
  9904.     if (dir == last)
  9905.       break;
  9906.   }
  9907.  
  9908.   last->next = NULL;
  9909.   last_include = last;
  9910. }
  9911.  
  9912. /* Add output to `deps_buffer' for the -M switch.
  9913.    STRING points to the text to be output.
  9914.    SPACER is ':' for targets, ' ' for dependencies.  */
  9915.  
  9916. static void
  9917. deps_output (string, spacer)
  9918.      char *string;
  9919.      int spacer;
  9920. {
  9921.   int size = strlen (string);
  9922.  
  9923.   if (size == 0)
  9924.     return;
  9925.  
  9926. #ifndef MAX_OUTPUT_COLUMNS
  9927. #define MAX_OUTPUT_COLUMNS 72
  9928. #endif
  9929.   if (MAX_OUTPUT_COLUMNS - 1 /*spacer*/ - 2 /*` \'*/ < deps_column + size
  9930.       && 1 < deps_column) {
  9931.     bcopy (" \\\n ", &deps_buffer[deps_size], 4);
  9932.     deps_size += 4;
  9933.     deps_column = 1;
  9934.     if (spacer == ' ')
  9935.       spacer = 0;
  9936.   }
  9937.  
  9938.   if (deps_size + size + 8 > deps_allocated_size) {
  9939.     deps_allocated_size = (deps_size + size + 50) * 2;
  9940.     deps_buffer = xrealloc (deps_buffer, deps_allocated_size);
  9941.   }
  9942.   if (spacer == ' ') {
  9943.     deps_buffer[deps_size++] = ' ';
  9944.     deps_column++;
  9945.   }
  9946.   bcopy (string, &deps_buffer[deps_size], size);
  9947.   deps_size += size;
  9948.   deps_column += size;
  9949.   if (spacer == ':') {
  9950.     deps_buffer[deps_size++] = ':';
  9951.     deps_column++;
  9952.   }
  9953.   deps_buffer[deps_size] = 0;
  9954. }
  9955.  
  9956. static void
  9957. fatal (PRINTF_ALIST (msg))
  9958.      PRINTF_DCL (msg)
  9959. {
  9960.   va_list args;
  9961.  
  9962.   fprintf (stderr, "%s: ", progname);
  9963.   VA_START (args, msg);
  9964.   vfprintf (stderr, msg, args);
  9965.   va_end (args);
  9966.   fprintf (stderr, "\n");
  9967.   exit (FATAL_EXIT_CODE);
  9968. }
  9969.  
  9970. /* More 'friendly' abort that prints the line and file.
  9971.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  9972.  
  9973. void
  9974. fancy_abort ()
  9975. {
  9976.   fatal ("Internal gcc abort.");
  9977. }
  9978.  
  9979. static void
  9980. perror_with_name (name)
  9981.      char *name;
  9982. {
  9983.   fprintf (stderr, "%s: ", progname);
  9984.   fprintf (stderr, "%s: %s\n", name, my_strerror (errno));
  9985.   errors++;
  9986. }
  9987.  
  9988. static void
  9989. pfatal_with_name (name)
  9990.      char *name;
  9991. {
  9992.   perror_with_name (name);
  9993. #ifdef VMS
  9994.   exit (vaxc$errno);
  9995. #else
  9996.   exit (FATAL_EXIT_CODE);
  9997. #endif
  9998. }
  9999.  
  10000. /* Handler for SIGPIPE.  */
  10001.  
  10002. static void
  10003. pipe_closed (signo)
  10004.      /* If this is missing, some compilers complain.  */
  10005.      int signo;
  10006. {
  10007.   fatal ("output pipe has been closed");
  10008. }
  10009.  
  10010. static void
  10011. memory_full ()
  10012. {
  10013.   fatal ("Memory exhausted.");
  10014. }
  10015.  
  10016.  
  10017. GENERIC_PTR
  10018. xmalloc (size)
  10019.      size_t size;
  10020. {
  10021.   register GENERIC_PTR ptr = (GENERIC_PTR) malloc (size);
  10022.   if (!ptr)
  10023.     memory_full ();
  10024.   return ptr;
  10025. }
  10026.  
  10027. static GENERIC_PTR
  10028. xrealloc (old, size)
  10029.      GENERIC_PTR old;
  10030.      size_t size;
  10031. {
  10032.   register GENERIC_PTR ptr = (GENERIC_PTR) realloc (old, size);
  10033.   if (!ptr)
  10034.     memory_full ();
  10035.   return ptr;
  10036. }
  10037.  
  10038. static GENERIC_PTR
  10039. xcalloc (number, size)
  10040.      size_t number, size;
  10041. {
  10042.   register size_t total = number * size;
  10043.   register GENERIC_PTR ptr = (GENERIC_PTR) malloc (total);
  10044.   if (!ptr)
  10045.     memory_full ();
  10046.   bzero (ptr, total);
  10047.   return ptr;
  10048. }
  10049.  
  10050. static char *
  10051. savestring (input)
  10052.      char *input;
  10053. {
  10054.   size_t size = strlen (input);
  10055.   char *output = xmalloc (size + 1);
  10056.   strcpy (output, input);
  10057.   return output;
  10058. }
  10059.  
  10060. /* Get the file-mode and data size of the file open on FD
  10061.    and store them in *MODE_POINTER and *SIZE_POINTER.  */
  10062.  
  10063. static int
  10064. file_size_and_mode (fd, mode_pointer, size_pointer)
  10065.      int fd;
  10066.      int *mode_pointer;
  10067.      long int *size_pointer;
  10068. {
  10069.   struct stat sbuf;
  10070.  
  10071.   if (fstat (fd, &sbuf) < 0) return (-1);
  10072.   if (mode_pointer) *mode_pointer = sbuf.st_mode;
  10073.   if (size_pointer) *size_pointer = sbuf.st_size;
  10074.   return 0;
  10075. }
  10076.  
  10077. static void
  10078. output_dots (fd, depth)
  10079.      FILE* fd;
  10080.      int depth;
  10081. {
  10082.   while (depth > 0) {
  10083.     putc ('.', fd);
  10084.     depth--;
  10085.   }
  10086. }
  10087.   
  10088.  
  10089. #ifdef VMS
  10090.  
  10091. /* Under VMS we need to fix up the "include" specification
  10092.    filename so that everything following the 1st slash is
  10093.    changed into its correct VMS file specification. */
  10094.  
  10095. static void
  10096. hack_vms_include_specification (fname)
  10097.      char *fname;
  10098. {
  10099.   register char *cp, *cp1, *cp2;
  10100.   int f, check_filename_before_returning, no_prefix_seen;
  10101.   char Local[512];
  10102.  
  10103.   check_filename_before_returning = 0;
  10104.   no_prefix_seen = 0;
  10105.  
  10106.   /* Ignore leading "./"s */
  10107.   while (fname[0] == '.' && fname[1] == '/') {
  10108.     strcpy (fname, fname+2);
  10109.     no_prefix_seen = 1;        /* mark this for later */
  10110.   }
  10111.   /* Look for the boundary between the VMS and UNIX filespecs */
  10112.   cp = rindex (fname, ']');    /* Look for end of dirspec. */
  10113.   if (cp == 0) cp = rindex (fname, '>'); /* ... Ditto            */
  10114.   if (cp == 0) cp = rindex (fname, ':'); /* Look for end of devspec. */
  10115.   if (cp) {
  10116.     cp++;
  10117.   } else {
  10118.     cp = index (fname, '/');    /* Look for the "/" */
  10119.   }
  10120.  
  10121.   /*
  10122.    * Check if we have a vax-c style '#include filename'
  10123.    * and add the missing .h
  10124.    */
  10125.   if (cp == 0) {
  10126.     if (index(fname,'.') == 0)
  10127.       strcat(fname, ".h");
  10128.   } else {
  10129.     if (index(cp,'.') == 0)
  10130.       strcat(cp, ".h");
  10131.   }
  10132.  
  10133.   cp2 = Local;            /* initialize */
  10134.  
  10135.   /* We are trying to do a number of things here.  First of all, we are
  10136.      trying to hammer the filenames into a standard format, such that later
  10137.      processing can handle them.
  10138.      
  10139.      If the file name contains something like [dir.], then it recognizes this
  10140.      as a root, and strips the ".]".  Later processing will add whatever is
  10141.      needed to get things working properly.
  10142.      
  10143.      If no device is specified, then the first directory name is taken to be
  10144.      a device name (or a rooted logical). */
  10145.  
  10146.   /* See if we found that 1st slash */
  10147.   if (cp == 0) return;        /* Nothing to do!!! */
  10148.   if (*cp != '/') return;    /* Nothing to do!!! */
  10149.   /* Point to the UNIX filename part (which needs to be fixed!) */
  10150.   cp1 = cp+1;
  10151.   /* If the directory spec is not rooted, we can just copy
  10152.      the UNIX filename part and we are done */
  10153.   if (((cp - fname) > 1) && ((cp[-1] == ']') || (cp[-1] == '>'))) {
  10154.     if (cp[-2] != '.') {
  10155.       /*
  10156.        * The VMS part ends in a `]', and the preceding character is not a `.'.
  10157.        * We strip the `]', and then splice the two parts of the name in the
  10158.        * usual way.  Given the default locations for include files in cccp.c,
  10159.        * we will only use this code if the user specifies alternate locations
  10160.        * with the /include (-I) switch on the command line.  */
  10161.       cp -= 1;            /* Strip "]" */
  10162.       cp1--;            /* backspace */
  10163.     } else {
  10164.       /*
  10165.        * The VMS part has a ".]" at the end, and this will not do.  Later
  10166.        * processing will add a second directory spec, and this would be a syntax
  10167.        * error.  Thus we strip the ".]", and thus merge the directory specs.
  10168.        * We also backspace cp1, so that it points to a '/'.  This inhibits the
  10169.        * generation of the 000000 root directory spec (which does not belong here
  10170.        * in this case).
  10171.        */
  10172.       cp -= 2;            /* Strip ".]" */
  10173.       cp1--; };            /* backspace */
  10174.   } else {
  10175.  
  10176.     /* We drop in here if there is no VMS style directory specification yet.
  10177.      * If there is no device specification either, we make the first dir a
  10178.      * device and try that.  If we do not do this, then we will be essentially
  10179.      * searching the users default directory (as if they did a #include "asdf.h").
  10180.      *
  10181.      * Then all we need to do is to push a '[' into the output string. Later
  10182.      * processing will fill this in, and close the bracket.
  10183.      */
  10184.     if (cp[-1] != ':') *cp2++ = ':'; /* dev not in spec.  take first dir */
  10185.     *cp2++ = '[';        /* Open the directory specification */
  10186.   }
  10187.  
  10188.   /* at this point we assume that we have the device spec, and (at least
  10189.      the opening "[" for a directory specification.  We may have directories
  10190.      specified already */
  10191.  
  10192.   /* If there are no other slashes then the filename will be
  10193.      in the "root" directory.  Otherwise, we need to add
  10194.      directory specifications. */
  10195.   if (index (cp1, '/') == 0) {
  10196.     /* Just add "000000]" as the directory string */
  10197.     strcpy (cp2, "000000]");
  10198.     cp2 += strlen (cp2);
  10199.     check_filename_before_returning = 1; /* we might need to fool with this later */
  10200.   } else {
  10201.     /* As long as there are still subdirectories to add, do them. */
  10202.     while (index (cp1, '/') != 0) {
  10203.       /* If this token is "." we can ignore it */
  10204.       if ((cp1[0] == '.') && (cp1[1] == '/')) {
  10205.     cp1 += 2;
  10206.     continue;
  10207.       }
  10208.       /* Add a subdirectory spec. Do not duplicate "." */
  10209.       if (cp2[-1] != '.' && cp2[-1] != '[' && cp2[-1] != '<')
  10210.     *cp2++ = '.';
  10211.       /* If this is ".." then the spec becomes "-" */
  10212.       if ((cp1[0] == '.') && (cp1[1] == '.') && (cp[2] == '/')) {
  10213.     /* Add "-" and skip the ".." */
  10214.     *cp2++ = '-';
  10215.     cp1 += 3;
  10216.     continue;
  10217.       }
  10218.       /* Copy the subdirectory */
  10219.       while (*cp1 != '/') *cp2++= *cp1++;
  10220.       cp1++;            /* Skip the "/" */
  10221.     }
  10222.     /* Close the directory specification */
  10223.     if (cp2[-1] == '.')        /* no trailing periods */
  10224.       cp2--;
  10225.     *cp2++ = ']';
  10226.   }
  10227.   /* Now add the filename */
  10228.   while (*cp1) *cp2++ = *cp1++;
  10229.   *cp2 = 0;
  10230.   /* Now append it to the original VMS spec. */
  10231.   strcpy (cp, Local);
  10232.  
  10233.   /* If we put a [000000] in the filename, try to open it first. If this fails,
  10234.      remove the [000000], and return that name.  This provides flexibility
  10235.      to the user in that they can use both rooted and non-rooted logical names
  10236.      to point to the location of the file.  */
  10237.  
  10238.   if (check_filename_before_returning && no_prefix_seen) {
  10239.     f = open (fname, O_RDONLY, 0666);
  10240.     if (f >= 0) {
  10241.       /* The file name is OK as it is, so return it as is.  */
  10242.       close (f);
  10243.       return;
  10244.     }
  10245.     /* The filename did not work.  Try to remove the [000000] from the name,
  10246.        and return it.  */
  10247.     cp = index (fname, '[');
  10248.     cp2 = index (fname, ']') + 1;
  10249.     strcpy (cp, cp2);        /* this gets rid of it */
  10250.   }
  10251.   return;
  10252. }
  10253. #endif    /* VMS */
  10254.  
  10255. #ifdef    VMS
  10256.  
  10257. /* These are the read/write replacement routines for
  10258.    VAX-11 "C".  They make read/write behave enough
  10259.    like their UNIX counterparts that CCCP will work */
  10260.  
  10261. static int
  10262. read (fd, buf, size)
  10263.      int fd;
  10264.      char *buf;
  10265.      int size;
  10266. {
  10267. #undef    read    /* Get back the REAL read routine */
  10268.   register int i;
  10269.   register int total = 0;
  10270.  
  10271.   /* Read until the buffer is exhausted */
  10272.   while (size > 0) {
  10273.     /* Limit each read to 32KB */
  10274.     i = (size > (32*1024)) ? (32*1024) : size;
  10275.     i = read (fd, buf, i);
  10276.     if (i <= 0) {
  10277.       if (i == 0) return (total);
  10278.       return (i);
  10279.     }
  10280.     /* Account for this read */
  10281.     total += i;
  10282.     buf += i;
  10283.     size -= i;
  10284.   }
  10285.   return (total);
  10286. }
  10287.  
  10288. static int
  10289. write (fd, buf, size)
  10290.      int fd;
  10291.      char *buf;
  10292.      int size;
  10293. {
  10294. #undef    write    /* Get back the REAL write routine */
  10295.   int i;
  10296.   int j;
  10297.  
  10298.   /* Limit individual writes to 32Kb */
  10299.   i = size;
  10300.   while (i > 0) {
  10301.     j = (i > (32*1024)) ? (32*1024) : i;
  10302.     if (write (fd, buf, j) < 0) return (-1);
  10303.     /* Account for the data written */
  10304.     buf += j;
  10305.     i -= j;
  10306.   }
  10307.   return (size);
  10308. }
  10309.  
  10310. /* The following wrapper functions supply additional arguments to the VMS
  10311.    I/O routines to optimize performance with file handling.  The arguments
  10312.    are:
  10313.      "mbc=16" - Set multi-block count to 16 (use a 8192 byte buffer).
  10314.      "deq=64" - When extending the file, extend it in chunks of 32Kbytes.
  10315.      "fop=tef"- Truncate unused portions of file when closing file.
  10316.      "shr=nil"- Disallow file sharing while file is open.
  10317.  */
  10318.  
  10319. static FILE *
  10320. freopen (fname, type, oldfile)
  10321.      char *fname;
  10322.      char *type;
  10323.      FILE *oldfile;
  10324. {
  10325. #undef    freopen    /* Get back the REAL fopen routine */
  10326.   if (strcmp (type, "w") == 0)
  10327.     return freopen (fname, type, oldfile, "mbc=16", "deq=64", "fop=tef", "shr=nil");
  10328.   return freopen (fname, type, oldfile, "mbc=16");
  10329. }
  10330.  
  10331. static FILE *
  10332. fopen (fname, type)
  10333.      char *fname;
  10334.      char *type;
  10335. {
  10336. #undef fopen    /* Get back the REAL fopen routine */
  10337.   /* The gcc-vms-1.42 distribution's header files prototype fopen with two
  10338.      fixed arguments, which matches ANSI's specification but not VAXCRTL's
  10339.      pre-ANSI implementation.  This hack circumvents the mismatch problem.  */
  10340.   FILE *(*vmslib_fopen)() = (FILE *(*)()) fopen;
  10341.  
  10342.   if (*type == 'w')
  10343.     return (*vmslib_fopen) (fname, type, "mbc=32",
  10344.                 "deq=64", "fop=tef", "shr=nil");
  10345.   else
  10346.     return (*vmslib_fopen) (fname, type, "mbc=32");
  10347. }
  10348.  
  10349. static int 
  10350. open (fname, flags, prot)
  10351.      char *fname;
  10352.      int flags;
  10353.      int prot;
  10354. {
  10355. #undef open    /* Get back the REAL open routine */
  10356.   return open (fname, flags, prot, "mbc=16", "deq=64", "fop=tef");
  10357. }
  10358.  
  10359. /* Avoid run-time library bug, where copying M out of N+M characters with
  10360.    N >= 65535 results in VAXCRTL's strncat falling into an infinite loop.
  10361.    gcc-cpp exercises this particular bug.  [Fixed in V5.5-2's VAXCRTL.]  */
  10362.  
  10363. static char *
  10364. strncat (dst, src, cnt)
  10365.      char *dst;
  10366.      const char *src;
  10367.      unsigned cnt;
  10368. {
  10369.   register char *d = dst, *s = (char *) src;
  10370.   register int n = cnt;    /* convert to _signed_ type */
  10371.  
  10372.   while (*d) d++;    /* advance to end */
  10373.   while (--n >= 0)
  10374.     if (!(*d++ = *s++)) break;
  10375.   if (n < 0) *d = '\0';
  10376.   return dst;
  10377. }
  10378.  
  10379. /* more VMS hackery */
  10380. #include <fab.h>
  10381. #include <nam.h>
  10382.  
  10383. extern unsigned long sys$parse(), sys$search();
  10384.  
  10385. /* Work around another library bug.  If a file is located via a searchlist,
  10386.    and if the device it's on is not the same device as the one specified
  10387.    in the first element of that searchlist, then both stat() and fstat()
  10388.    will fail to return info about it.  `errno' will be set to EVMSERR, and
  10389.    `vaxc$errno' will be set to SS$_NORMAL due yet another bug in stat()!
  10390.    We can get around this by fully parsing the filename and then passing
  10391.    that absolute name to stat().
  10392.  
  10393.    Without this fix, we can end up failing to find header files, which is
  10394.    bad enough, but then compounding the problem by reporting the reason for
  10395.    failure as "normal successful completion."  */
  10396.  
  10397. #undef fstat    /* get back to library version */
  10398.  
  10399. static int
  10400. VMS_fstat (fd, statbuf)
  10401.      int fd;
  10402.      struct stat *statbuf;
  10403. {
  10404.   int result = fstat (fd, statbuf);
  10405.  
  10406.   if (result < 0)
  10407.     {
  10408.       FILE *fp;
  10409.       char nambuf[NAM$C_MAXRSS+1];
  10410.  
  10411.       if ((fp = fdopen (fd, "r")) != 0 && fgetname (fp, nambuf) != 0)
  10412.     result = VMS_stat (nambuf, statbuf);
  10413.       /* No fclose(fp) here; that would close(fd) as well.  */
  10414.     }
  10415.  
  10416.   return result;
  10417. }
  10418.  
  10419. static int
  10420. VMS_stat (name, statbuf)
  10421.      const char *name;
  10422.      struct stat *statbuf;
  10423. {
  10424.   int result = stat (name, statbuf);
  10425.  
  10426.   if (result < 0)
  10427.     {
  10428.       struct FAB fab;
  10429.       struct NAM nam;
  10430.       char exp_nam[NAM$C_MAXRSS+1],  /* expanded name buffer for sys$parse */
  10431.        res_nam[NAM$C_MAXRSS+1];  /* resultant name buffer for sys$search */
  10432.  
  10433.       fab = cc$rms_fab;
  10434.       fab.fab$l_fna = (char *) name;
  10435.       fab.fab$b_fns = (unsigned char) strlen (name);
  10436.       fab.fab$l_nam = (void *) &nam;
  10437.       nam = cc$rms_nam;
  10438.       nam.nam$l_esa = exp_nam,  nam.nam$b_ess = sizeof exp_nam - 1;
  10439.       nam.nam$l_rsa = res_nam,  nam.nam$b_rss = sizeof res_nam - 1;
  10440.       nam.nam$b_nop = NAM$M_PWD | NAM$M_NOCONCEAL;
  10441.       if (sys$parse (&fab) & 1)
  10442.     {
  10443.       if (sys$search (&fab) & 1)
  10444.         {
  10445.           res_nam[nam.nam$b_rsl] = '\0';
  10446.           result = stat (res_nam, statbuf);
  10447.         }
  10448.       /* Clean up searchlist context cached by the system.  */
  10449.       nam.nam$b_nop = NAM$M_SYNCHK;
  10450.       fab.fab$l_fna = 0,  fab.fab$b_fns = 0;
  10451.       (void) sys$parse (&fab);
  10452.     }
  10453.     }
  10454.  
  10455.   return result;
  10456. }
  10457. #endif /* VMS */
  10458.